简体   繁体   English

使用我自己的数据的Tensorflow矩阵大小错误

[英]Tensorflow matrix size error using my own data

I'm begginner of tensorflow. 我是张量流的始作俑者。 I wanted to use with my own medical raw image data and make simple autoencorder but I fail.I guess marix size is wrong. 我想使用自己的医学原始图像数据并进行简单的自动编码,但失败了。我猜想marix大小是错误的。 This is perhaps a noob question but I can't figure it out. 这也许是一个菜鸟问题,但我无法弄清楚。

my image data size is 512*512*1 and dtype is int16. 我的图像数据大小为512 * 512 * 1,dtype为int16。


error 错误

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].

my code 我的密码

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()})

The dimensions of x, b_enc, d_dec were wrong. x,b_enc和d_dec的尺寸不正确。 Please see the working version below. 请参阅下面的工作版本。 In general, it is useful first to implement the algorithm in numpy and then port to tensorflow if you begin with TF. 通常,首先使用numpy实现该算法很有用,然后如果您以TF开头则移植到tensorflow。

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!')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM