cat articles/numpy-cast

NumPy cast overflow behavior can vary by environment and array size

I got caught by exactly what the title says: overflow behavior when casting in NumPy differed depending on the environment and the size of the data. It seems to take a different code path depending on the array length, and it took me a while to identify the cause. The correct answer is probably "do not pass overflowing data into a cast", but if behavior changes like this, I would at least appreciate a warning that an overflow happened.

This feels like the kind of bug that can look fine during development on a Mac, while already being broken, and then behave differently in production. I did not dig far enough to know whether this is specific to the Mac environment or whether it depends on the BLAS implementation, such as Intel MKL.

import platform
print(platform.system())
# オーバーフローして1になる
print(np.array([257.0], dtype="float32").astype('uint8'))
# オーバーフローして1になる
print(np.array([257.0, 0, 0, 0, 0, 0, 0], dtype="float32").astype('uint8'))
# オーバーフローして1になるが正しい、と思いきや、環境によっては丸め込まれて255になる
print(np.array([257.0, 0, 0, 0, 0, 0, 0, 0], dtype="float32").astype('uint8'))
Linux
[1]
[1 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0]
Windows
[1]
[1 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0]

On my Intel Mac, the value is clamped instead.

Darwin
[1]
[1 0 0 0 0 0 0]
[255   0   0   0   0   0   0   0]

cat related_articles/numpy-cast.yaml

  1. Building a simple fully connected neural network with TensorFlow 2 without KerasA hands-on note implementing a simple feed-forward neural network with only TensorFlow APIs, without Keras, to understand layers, activation functions, losses, automatic differentiation, and manual training.
  2. Solo silver medal, 43rd place, in Kaggle Feedback Prize - Predicting Effective ArgumentsI joined Feedback Prize - Predicting Effective Arguments solo, finished 43rd out of 1,566 teams, and wrote down what worked, what failed, and what solo participation felt like.
  3. Understanding LangChain Expression Language (LCEL)LCEL is LangChain's recommended way to build chains. This article explains the basic behavior of Runnable, RunnableSequence, RunnableParallel, dict syntax, invoke, and RunnablePassthrough step by step.