形状必须是等级1,但是等级2 tflearn错误

时间:2017-11-11 01:48:40

标签: python-3.x machine-learning tensorflow neural-network tflearn

我正在使用tflearn提供的DNN来学习一些数据。我的data变量的形状为(6605, 32),我的labels数据的形状为(6605,),我在下面的代码中重塑为(6605, 1) ... < / p>

# Target label used for training
labels = np.array(data[label], dtype=np.float32)

# Reshape target label from (6605,) to (6605, 1)
labels = tf.reshape(labels, shape=[-1, 1])

# Data for training minus the target label.
data = np.array(data.drop(label, axis=1), dtype=np.float32)

# DNN
net = 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)

# Define model.
model = tflearn.DNN(net)
model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)

这给了我一些错误,第一个是......

  

tensorflow.python.framework.errors_impl.InvalidArgumentError:Shape必须是等级1,但对于&#39; strided_slice&#39;是等级2 (op:&#39; StridedSlice&#39;)输入形状:[6605,1],[1,16],[1,16],[1]。

......第二个是......

  

在处理上述异常期间,发生了另一个异常:

     

ValueError:Shape必须为1级,但strig_slice&#39;为#2; (op:&#39; StridedSlice&#39;)输入形状:[6605,1],[1,16],[1,16],[1]。

我不知道rank 1rank 2是什么,所以我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:2)

在Tensorflow中,rank是张量的维数(与矩阵秩不相似)。例如,跟随张量的等级为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之上构建的,因此输入不应该是张量。我已按如下方式修改了您的代码,并在必要时进行了评论。

# Target label used for training
labels = np.array(data[label], dtype=np.float32)

# Reshape target label from (6605,) to (6605, 1)
labels =np.reshape(labels,(-1,1)) #makesure the labels has the shape of (?,1)

# Data for training minus the target label.
data = np.array(data.drop(label, axis=1), dtype=np.float32)
data = np.reshape(data,(-1,32)) #makesure the data has the shape of (?,32)

# DNN
net = 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)

# Define model.
model = tflearn.DNN(net)
model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)

希望这有帮助。

相关问题