技术问询:axis与dim关键字参数的使用存在哪些差异?
Great question! This is a super common point of confusion when jumping between different frameworks or even different APIs within the same framework—let’s unpack it clearly.
At their core, axis and dim refer to exactly the same thing: they specify which dimension of a tensor you want an operation to run along. The difference is purely a naming convention, not a functional one.
Here’s how this plays out in common tools:
- TensorFlow: You’ll see both used, but there’s a trend toward standardizing on
axisin newer APIs. For example, the oldtf.nn.softmaxusesdim, but modern functions liketf.reduce_meanortf.argmaxprioritizeaxis(though many still acceptdimas a legacy alias). Some TF docs even markdimas deprecated, so it’s safer to useaxisfor new code. - PyTorch: The framework consistently uses
dimacross almost all its operations—you’ll rarely seeaxishere. Sotorch.softmax(input, dim=-1)does the exact same thing as TensorFlow’s version, just with the parameter named differently. - NumPy: It’s always used
axis, which is probably where the convention originated. Most early deep learning frameworks took inspiration from NumPy, soaxiswas the first common term.
A quick note to avoid mistakes: Even though they mean the same thing, you can’t pass both parameters to the same function. For example, tf.nn.softmax(logits, axis=-1, dim=-1) will throw an error—pick one (preferably the one the docs recommend for that specific function).
In short: No functional difference, just different names for the same concept. Follow the naming convention of the API you’re using, and you’ll be good to go!
内容的提问来源于stack exchange,提问作者figs_and_nuts

