2017-11-13 12 views
1

私が例からTensorFlowを勉強:私は私自身のカスタム画像を使用しています、そのチュートリアルは異なりMNIST Tutorial - Julia.jlMethodError:いいえ方法マッチングreduce_sum(:: TensorFlow.Tensor {FLOAT32}; reduction_indicesは= [2])

32X32(RGB))のフォーマットは、cifar-10のように変化します。 csvの各行は3073列で、最後のラベルはラベルです。そして、私はトレーニングのための最初の240行とテストのための残りの240行を選択するために配列スライスを使用した300行を持っています。 そこでナットシェルでIは4つの配列を有する: images_train = 240 X 3072 labels_train = 240 X 1 images_test = X 3072 60 labels_test =以下、X 1 60は、誤りである: THE ERROR

コード:

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 = 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([7*7*64, 1024]) 
b_fc1 = bias_variable([1024]) 

h_pool2_flat = reshape(h_pool2, [-1, 7*7*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)), reduction_indices=[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, initialize_all_variables()) 

for i in 1:40 
    images_train,labels_train = next_batch(16) # randomly generate batches from training dataset 
    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 = load_test_set() # 60 X 3072, 60 X 1 Arrays 

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

はありがとう

あなたのような、 axisにそれを変更する必要があり reduce_sumreduction_indices引数はありません

答えて

1

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

あなたの答えは本当に便利だったと実行して、再形成のためにお願いしました:INFO:ステップ1、トレーニング精度を0.0 Tensorflowエラー:ステータス:再構築への入力は、65536個の値を持つテンソルであるが、要求された形状は、複数が必要です3136 \t [ノード:Reshape_2 = Reshape [T = DT_FLOAT、Tshape = DT_INT32、_class = []、_device = "/ job:localhost/replica:0/task:0/cpu:0"](MaxPool_2、Const_8 )]]] – spg

+0

@spgこれは入力画像のサイズを変更したためです。サイズが一致しないためです。ちょうどW_fc1をW_fc1 = weight_variable([8 * 8 * 64、1024])に、h_pool2_flatをh_pool2_flat = reshape(h_pool2、[-1、8 * 8 * 64])に変更してください。 – asakryukin

+0

私は今述べた反復のために正確さが常にあったけれども、私は今トレーニングをします。ありがとうございました^ n – spg

関連する問題