第一次遇到这样的问题。测试tflearn的神经网络会产生错误。尝试测试此代码时,Python会生成错误。 conv_2d没有这样的问题。
我的代码:
import numpy as np
import random
import tflearn
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.conv import conv_3d, max_pool_3d
from tflearn.layers.estimator import regression
trainX = [[[random.randint(0,3) for col in range(15)] for row in range(15)] for x in range(50)]
testX = [[[random.randint(0,3) for col in range(15)] for row in range(15)] for x in range(10)]
trainY = [[0,1] for x in range(100)]
testY = [[0,1] for x in range(10)]
idnn = 'test_cnn'
network = input_data(shape=[None, 15, 15,15, 1])
network = conv_3d(network, 10, 3, activation='relu')
network = max_pool_3d(network, 2)
network = conv_3d(network, 32, 3, activation='relu')
network = conv_3d(network, 32, 3, activation='relu')
network = max_pool_3d(network, 2)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 2, activation='softmax')
network = regression(network, optimizer='adam',
loss='categorical_crossentropy',
learning_rate=0.001)
# Train using classifier
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit(trainX, trainY, n_epoch=10, shuffle=True, validation_set=(testX, testY),
show_metric=True, batch_size=5, run_id= idnn)
pred = model.predict(testX)
trainX = [[[random.randint(0,3) for col in range(15)] for row in range(15)] for x in range(50)]
testX = [[[random.randint(0,3) for col in range(15)] for row in range(15)] for x in range(10)]
trainY = [[0,1] for x in range(100)]
testY = [[0,1] for x in range(10)]
idnn = 'test_cnn'
network = input_data(shape=[None, 15, 15,15, 1])
network = conv_3d(network, 10, 3, activation='relu')
network = max_pool_3d(network, 2)
network = conv_3d(network, 32, 3, activation='relu')
network = conv_3d(network, 32, 3, activation='relu')
network = max_pool_3d(network, 2)
network = fully_connected(network, 512, activation='relu')
network = dropout(network, 0.5)
network = fully_connected(network, 2, activation='softmax')
network = regression(network, optimizer='adam',
loss='categorical_crossentropy',
learning_rate=0.001)
# Train using classifier
model = tflearn.DNN(network, tensorboard_verbose=0)
model.fit(trainX, trainY, n_epoch=10, shuffle=True, validation_set=(testX, testY),
show_metric=True, batch_size=5, run_id= idnn)
pred = model.predict(testX)
这在尝试测试代码时会出现错误。
ValueError: Cannot feed value of shape (50, 15, 15) for Tensor 'InputData/X:0', which has shape '(?, 15, 15, 15, 1)'
可能是什么问题?请有人帮忙。
答案 0 :(得分:0)
您的trainX
和testX
有形状(50,15,15)。但是,在
network = input_data(shape=[None, 15, 15,15, 1])
您指定输入的形状为(None,15,15,15,1)。这些形状不匹配。 None
可以表示任何数字,但形状应匹配。
例如,您可以将trainX
和testX
变形(50,15,15,15,1)。
答案 1 :(得分:0)
让我们看看,conv_3d网络是什么?
来自tflearn文档的信息。
输入
5-D Tensor [batch, in_depth, in_height, in_width, in_channels]
。
输出
5-D Tensor [filter_depth, filter_height, filter_width, in_channels, out_channels].
trainX
和testX
数据的大小应相同。因为您使用testX
中的validation_set
数据。
您的trainX
和testX
数据看起来像2D数据集。 50x(15,15)
,10x(15,15)
如果您想使用conv_3d,则必须使train
和test
数据的大小相同。 (50x(15,15)
,50x(15,15)
)。
此外,我发现您的目标数据trainY
与您的列车数据trainX
的大小不同。