我正在学习张力课程,其中一个步骤是重塑一个形状为(60000,10,1)形状(60000,10)的numpy数组。但是在调用ndarray.reshape方法之后看起来形状没有改变 - 它仍然是(60000,10,1)。
但是,我试图直接设置形状属性,它的工作原理!喜欢:
# Setting shape attribute works - fragment of python code
# Shape of training[1] is (60000, 10, 1)
training[1] = np.array([vectorized_result(y) for y in training[1]])
training[1].shape = (training[1].shape[0],training[1].shape[1])
# print the shape
print(training[1].shape)
# definition of vectorized_result
def vectorized_result(j):
"""Return a 10-dimensional unit vector with a 1.0 in the jth
position and zeroes elsewhere. This is used to convert a digit
(0...9) into a corresponding desired output from the neural
network."""
e = np.zeros((10, 1))
e[j] = 1.0
return e
运行代码,然后获取
$ python test.py
(60000, 10)
使用reshape方法,但失败:
# fragment of python code
training[1] = np.array([vectorized_result(y) for y in training[1]])
training[1].reshape((training[1].shape[0],training[1].shape[1]))
# print the shape
print(training[1].shape)
# definition of vectorized_result
def vectorized_result(j):
"""Return a 10-dimensional unit vector with a 1.0 in the jth
position and zeroes elsewhere. This is used to convert a digit
(0...9) into a corresponding desired output from the neural
network."""
e = np.zeros((10, 1))
e[j] = 1.0
return e
运行代码,然后获取
$ python test.py
(60000, 10, 1)
请帮我解决这个问题。
非常感谢。
尝试以下更改:
training[1] = training[1].reshape((training[1].shape[0],training[1].shape[1]))