2016-12-07 9 views
0

私はテンソルフローを始めています。私は自分自身の医療用の生の画像データを使い、シンプルなオートエンコーダーを作成したかったのですが、失敗しました。私はmarixサイズが間違っていると思います。これはおそらくノブの質問ですが、私はそれを理解することはできません。私自身のデータを使ったTensorflow行列サイズの誤差

私の画像のデータサイズは512 * 512 * 1で、dtypeはint16です。


エラー

Traceback (most recent call last): 
File "/Users/tk/Desktop/tensorflow_test/test.py", line 17, in <module> 
decoded = tf.sigmoid(tf.matmul(w_dec,encoded) + b_dec) 
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 814, in binary_op_wrapper 
return func(x, y, name=name) 
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 73, in add 
result = _op_def_lib.apply_op("Add", x=x, y=y, name=name) 
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 759, in apply_op 
op_def=op_def) 
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2242, in create_op 
set_shapes_for_outputs(ret) 
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1617, in set_shapes_for_outputs 
shapes = shape_func(op) 
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1568, in call_with_requiring 
return call_cpp_shape_fn(op, require_shape_fn=True) 
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 610, in call_cpp_shape_fn 
debug_python_shape_fn, require_shape_fn) 
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 675, in _call_cpp_shape_fn_impl 
raise ValueError(err.message) 
ValueError: Dimensions must be equal, but are 625 and 262144 for 'add_1' (op: 'Add') with input shapes: [262144,625], [262144]. 

私のコードのx、b_encの

import tensorflow as tf 
import numpy as np 

#load_image 
temp = np.fromfile('/Users/learn.raw',np.int16) 
input_image = tf.to_float(temp) 

# Variables 
x = tf.placeholder("float",None) 
w_enc = tf.Variable(tf.random_normal([625, 262144], mean=0.0, stddev=0.05)) 
w_dec = tf.Variable(tf.random_normal([262144, 625], mean=0.0, stddev=0.05)) 
b_enc = tf.Variable(tf.zeros([625])) 
b_dec = tf.Variable(tf.zeros([262144])) 

#model 
encoded = tf.sigmoid(tf.matmul(w_enc,x) + b_enc) 
decoded = tf.sigmoid(tf.matmul(w_dec,encoded) + b_dec) 


# Cost Function 
cross_entropy = -1. * x * tf.log(decoded) - (1. - x) * tf.log(1. - decoded) 
loss = tf.reduce_mean(cross_entropy) 
train_step = tf.train.AdagradOptimizer(0.1).minimize(loss) 

# Train 
init = tf.initialize_all_variables() 

with tf.Session() as sess: 
    sess.run(init) 
    print('Training...') 
    train_step.run(feed_dict = {x: input_image.eval()}) 

答えて

0

寸法は、間違っていd_decました。下記の作業バージョンをご覧ください。一般に、まずnumpyでアルゴリズムを実装し、次にTFで始める場合はテンソルフローに移植すると便利です。

import tensorflow as tf 
import numpy as np 

tf.reset_default_graph() 

#load_image 
image_size = 16 
k = 64 
temp = np.zeros((image_size, image_size)) 


# Variables 
x_placeholder = tf.placeholder("float", (image_size, image_size)) 

x = tf.reshape(x_placeholder, [image_size * image_size, 1]) 
w_enc = tf.Variable(tf.random_normal([k, image_size * image_size], mean=0.0, stddev=0.05)) 
w_dec = tf.Variable(tf.random_normal([image_size * image_size, k], mean=0.0, stddev=0.05)) 
b_enc = tf.Variable(tf.zeros([k, 1])) 
b_dec = tf.Variable(tf.zeros([image_size * image_size, 1])) 

#model 
encoded = tf.sigmoid(tf.matmul(w_enc, x) + b_enc) 
decoded = tf.sigmoid(tf.matmul(w_dec,encoded) + b_dec) 


# Cost Function 
cross_entropy = -1. * x * tf.log(decoded) - (1. - x) * tf.log(1. - decoded) 
loss = tf.reduce_mean(cross_entropy) 
train_step = tf.train.AdagradOptimizer(0.1).minimize(loss) 

# Train 
init = tf.global_variables_initializer() 

with tf.Session() as sess: 
    sess.run(init) 
    print('Training...') 
    for _ in xrange(10): 
     loss_val, _ = sess.run([loss, train_step], feed_dict = {x_placeholder: temp}) 
     print loss_val 
    print('Done!') 
関連する問題