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

Bol*_*boa 2 machine-learning neural-network python-3.x tensorflow tflearn

我正在使用 提供的 DNNtflearn从一些数据中学习。我的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)
Run Code Online (Sandbox Code Playgroud)

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

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 1什么rank 2,所以我不知道如何解决这个问题。

Nip*_*hne 5

在 Tensorflow 中,秩是张量的维数(与矩阵秩不同)。例如,以下张量的秩为 2。

t1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(t1.shape) # prints (3, 3)
Run Code Online (Sandbox Code Playgroud)

此外,以下张量的秩为 3。

t2 = np.array([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
print(t2.shape) # prints (2, 2, 3)
Run Code Online (Sandbox Code Playgroud)

由于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)
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。