【问题标题】:ERROR: LoadError: Tensorflow error: Status: Incompatible shapes: [16] vs. [16,9]错误:LoadError:Tensorflow 错误:状态:不兼容的形状:[16] 与 [16,9]
【发布时间】:2017-11-15 11:10:43
【问题描述】:

实现一个改编自multilayer CNN Tutorial的模型

我正在通过以下示例学习 TensorFlow:MNIST 教程 - Julia.jl

与他们的教程不同,我使用的是我自己的自定义图像(32 X 32 (RGB))格式,类似于 cifar-10。 csv 中的每一行是 3073 列,最后一列是标签。

我有 300 行,我使用数组切片来选择前 240 行进行训练,其余行用于测试。所以简而言之,我有 4 个数组:

images_train = 240 X 3072 labels_train = 240 X 1 images_test = 60 X 3072 labels_test = 60 X 1 .

当我尝试训练网络时出现错误,

不兼容的形状:[16] 与 [16,9]

实际上,我有 9 个类,包含 240 个训练集和 60 个用于测试的图像。

代码是:

using TensorFlow
using Distributions
include("loader.jl") 

session = Session(Graph())
function weight_variable(shape)
    initial = map(Float32, rand(Normal(0, .001), shape...))
    return Variable(initial)
end

function bias_variable(shape)
    initial = fill(Float32(.1), shape...)
    return Variable(initial)
end

function conv2d(x, W)
    nn.conv2d(x, W, [1, 1, 1, 1], "SAME")

end

function max_pool_2x2(x)
    nn.max_pool(x, [1, 2, 2, 1], [1, 2, 2, 1], "SAME")
end

x = placeholder(Float32)
y_ = placeholder(Float32)

W_conv1 = weight_variable([5, 5, 3, 32]) 
b_conv1 = bias_variable([32])

x_image = x #reshape(x, [-1, 32, 32, 3])

h_conv1 = nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])

h_conv2 = nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

W_fc1 = weight_variable([8*8*64, 1024]) #
b_fc1 = bias_variable([1024])

h_pool2_flat = reshape(h_pool2, [-1, 8*8*64])
h_fc1 = nn.relu(h_pool2_flat * W_fc1 + b_fc1)

keep_prob = placeholder(Float32)
h_fc1_drop = nn.dropout(h_fc1, keep_prob)

W_fc2 = weight_variable([1024, 9])
b_fc2 = bias_variable([9])

y_conv = nn.softmax(h_fc1_drop * W_fc2 + b_fc2)


cross_entropy = reduce_mean(-reduce_sum((y_ .* log(y_conv)), axis=[2]))

train_step = train.minimize(train.AdamOptimizer(1e-4), cross_entropy)

correct_prediction = indmax(y_conv, 2) .== indmax(y_, 2)

accuracy = reduce_mean(cast(correct_prediction, Float32))

run(session, global_variables_initializer())

for i in 1:100
    images_train,labels_train = batching(16) # randomly generate batches from training dataset (16,32,32,3)
    if i%4 == 1
        train_accuracy = run(session, accuracy, Dict(x=>images_train, y_=>labels_train, keep_prob=>1.0))
        info("step $i, training accuracy $train_accuracy")
    end
    run(session, train_step, Dict(x=>images_train, y_=>labels_train, keep_prob=>.5))
end

images_test, labels_test = testloader() # (60, 33,32,3), (60,) Arrays


test_accuracy = run(session, accuracy, Dict(x=>images_test, y_=>labels_test, keep_prob=>1.0))
info("test accuracy $test_accuracy")

【问题讨论】:

    标签: tensorflow julia conv-neural-network mnist


    【解决方案1】:

    申请

    重塑(A,(16,1))

    解决了不兼容的问题。

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 2015-07-02
      • 2016-04-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多