【问题标题】:I got new questions NumPy dimension我有新问题 NumPy 维度
【发布时间】:2020-03-02 07:34:51
【问题描述】:

我有一个关于 Keras 和许多其他应用程序中经常使用的 Numpy 维度的问题

问题 1

shape=(10,1) 和 (10,) 有什么区别?

问题 2

在使用 NumPy 的某些应用程序中什么是“无”, 例如)输入形状=(无,32) 是不是说具体尺寸还没有设置好?

问题 3

我该如何解释: ...轴=-1 如何将轴设置为-1?

【问题讨论】:

  • None 不是有效的 numpy 维度。 tensorflow 等其他软件包确实使用它。
  • 在Python中,-1常用来表示最后一个元素或最后一个维度
  • (10,1) 是一个二维数组,(10,) 1d。它们可以相互重塑。您会看到显示、索引、广播等方面的差异。
  • 关于input_shape=(None, 32),你可能指的是Keras层中的input_shape参数。在这种情况下,它是一个 TensorFlow 形状,这意味着批次中的每个示例都具有未指定大小的第一维和 32 个元素的第二维(通常它表示序列数据)。与 NumPy 不同的是,在 TensorFlow(non-eager 模式,张量是象征性的)中,None 可以用来表示某些张量维度的大小是不预先固定的。
  • @Jim Kim,如果我已经回答了你的问题,请你接受答案并投票。谢谢。

标签: python numpy tensorflow keras data-science


【解决方案1】:

1) 答:形状为 (10000,) 的数组是一维数组,而形状为 (10000,1) 的数组是二维数组

import numpy as np

# Array with shape (10000,) is a 1-Dimensional Array
one=np.ones((10,))
print("1-Dimensional Array:\n",one)

#Array with shape (10000,1) is a 2-Dimensional Array.
two=np.ones((10,1)) 
print("2-Dimensional Array:\n",two)

输出:

1-Dimensional Array:
 [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]

2-Dimensional Array:
 [[1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [1.]
 [1.]]

2) 答案: None 只是为数组对象添加一个维度。您可以使用“None”或 numpy 的“newaxis”来创建新维度。

一般提示:您也可以使用 None 代替 np.newaxis

import numpy as np

#### Import the Fashion MNIST dataset
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()

#Original Dimension
print(train_images.shape)

train_images1 = train_images[None,:,:,:]
#Add Dimension using None
print(train_images1.shape)

train_images2 = train_images[np.newaxis is None,:,:,:]
#Add dimension using np.newaxis
print(train_images2.shape)

#np.newaxis and none are same
np.newaxis is None

输出:

(60000, 28, 28)
(1, 60000, 28, 28)
(1, 60000, 28, 28)
True

3) 答案: axis 参数指定新轴在结果维度中的索引。例如,如果axis=0,它将是第一个维度,如果axis=-1,它将是最后一个维度。

# input array 
a = np.array([[ 1, 2, 3], [ -1, -2, -3]] ) 
print ("1st Input array : \n", a)  

b = np.array([[ 4, 5, 6], [ -4, -5, -6]] ) 
print ("2nd Input array : \n", b)  

# Stacking the two arrays along axis 0 
out1 = np.stack((a, b), axis = 0) 
print ("Output array along axis 0:\n ", out1) 

# Stacking the two arrays along axis 1 
out2 = np.stack((a, b), axis = 1) 
print ("Output array along axis 1:\n ", out2) 

# Stacking the two arrays along axis -1 
out3 = np.stack((a, b), axis = -1) 
print ("Output array along axis -1:\n ", out3)

输出:

1st Input array : 
 [[ 1  2  3]
 [-1 -2 -3]]

2nd Input array : 
 [[ 4  5  6]
 [-4 -5 -6]]

Output array along axis 0:
  [[[ 1  2  3]
  [-1 -2 -3]]

 [[ 4  5  6]
  [-4 -5 -6]]]

Output array along axis 1:
  [[[ 1  2  3]
  [ 4  5  6]]

 [[-1 -2 -3]
  [-4 -5 -6]]]

Output array along axis -1:
  [[[ 1  4]
  [ 2  5]
  [ 3  6]]

 [[-1 -4]
  [-2 -5]
  [-3 -6]]]

【讨论】:

  • 非常感谢! :=) 非常有帮助.. 非常感谢您的回答,非常感谢!
  • @Jim Kim,如果我已经回答了您的问题,请您接受并投票。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多