【问题标题】:Shape must be rank 1 but is rank 2 tflearn error形状必须为 1 级,但为 2 级 tflearn 错误
【发布时间】:2018-04-24 07:37:27
【问题描述】:

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

# 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:形状必须为 1 级,但对于 'strided_slice'(操作:'StridedSlice')为 2 级,输入形状为:[6605,1]、[1,16]、[ 1,16],[1]。

...第二个是...

在处理上述异常的过程中,又发生了一个异常:

ValueError: 形状必须为 1 级,但对于输入形状为 [6605,1]、[1,16]、[1,16]、[1] 的“strided_slice”(操作:“StridedSlice”)为 2 级.

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

【问题讨论】:

  • 尝试删除labels的整形;错误是否仍然存在?是的,请提供您的数据样本(此外,如您所说,提供此模型的任何链接都会很有用)

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


【解决方案1】:

在 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 之上,因此输入不应是张量。我已将您的代码修改如下,并在必要时进行了注释。

# 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)

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-10
    • 1970-01-01
    • 2018-05-30
    • 1970-01-01
    相关资源
    最近更新 更多