我使用tflearn
提供的DNN来学习一些数据。我的data
变量的形状是(6605, 32)
,我的labels
数据的形状是(6605,)
,我在下面的代码中将其重塑为(6605, 1)
…
# 用于训练的目标标签labels = np.array(data[label], dtype=np.float32)# 将目标标签从(6605,)重塑为(6605, 1)labels = tf.reshape(labels, shape=[-1, 1])# 用于训练的数据,去掉目标标签data = np.array(data.drop(label, axis=1), dtype=np.float32)# DNNnet = tflearn.input_data(shape=[None, 32])net = tflearn.fully_connected(net, 32)net = tflearn.fully_connected(net, 32)net = tflearn.fully_connected(net, 1, activation='softmax')net = tflearn.regression(net)# 定义模型model = tflearn.DNN(net)model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)
这让我遇到了几个错误,第一个是…
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 1 but is rank 2 for ‘strided_slice’ (op: ‘StridedSlice’) with input shapes: [6605,1], [1,16], [1,16], [1].
…第二个是…
During handling of the above exception, another exception occurred:
ValueError: Shape must be rank 1 but is rank 2 for ‘strided_slice’ (op: ‘StridedSlice’) with input shapes: [6605,1], [1,16], [1,16], [1].
我不知道rank 1
和rank 2
是什么意思,所以我不知道如何解决这个问题。
回答:
在Tensorflow中,秩是张量的维度数(与矩阵的秩不同)。例如,以下张量的秩为2。
t1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])print(t1.shape) # prints (3, 3)
此外,以下张量的秩为3。
t2 = np.array([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])print(t2.shape) # prints (2, 2, 3)
由于tflearn是建立在Tensorflow之上的,输入不应该是张量。我已经修改了你的代码,并在必要的地方加了注释。
# 用于训练的目标标签labels = np.array(data[label], dtype=np.float32)# 将目标标签从(6605,)重塑为(6605, 1)labels =np.reshape(labels,(-1,1)) #确保labels的形状为(?,1)# 用于训练的数据,去掉目标标签data = np.array(data.drop(label, axis=1), dtype=np.float32)data = np.reshape(data,(-1,32)) #确保data的形状为(?,32)# DNNnet = tflearn.input_data(shape=[None, 32])net = tflearn.fully_connected(net, 32)net = tflearn.fully_connected(net, 32)net = tflearn.fully_connected(net, 1, activation='softmax')net = tflearn.regression(net)# 定义模型model = tflearn.DNN(net)model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)
希望这对你有帮助。