我正在通过给两个占位符存储以下内容来训练autoencoder
:
x1 = [x1]
X = [x1,x2,x3...xn]
它认为:
y1 = W*x1 + b_encoding1
因此,我有一个名为b_encoder1
的变量(b)
(打印时,我得到:<tf.Variable 'b_encoder1:0' shape=(10,) dtype=float32_ref>
)
但它也认为:
Y = W*X + b_encoding1
第二个b_encoding1
的大小必须为(10,n)
的{{1}}的整数。如何增加它并在(10,)
中传递它?
tensorflow
整个代码如下:
Y = tf.compat.v1.nn.xw_plus_b(X, W1, b_encoder1, name='Y')
我还声明了损失函数,依此类推,然后进行以下训练:
x1 = tf.compat.v1.placeholder( tf.float32, [None,input_shape], name = 'x1')
X = tf.compat.v1.placeholder( tf.float32, [None,input_shape,sp], name = 'X')
W1 = tf.Variable(tf.initializers.GlorotUniform()(shape=[input_shape,code_length]),name='W1')
b_encoder1 = tf.compat.v1.get_variable(name='b_encoder1',shape=[code_length],initializer=tf.compat.v1.initializers.zeros(), use_resource=False)
K = tf.Variable(tf.initializers.GlorotUniform()(shape=[code_length,code_length]),name='K')
b_decoder1 = tf.compat.v1.get_variable(name='b_decoder1',shape=[input_shape],initializer=tf.compat.v1.initializers.zeros(), use_resource=False)
y1 = tf.compat.v1.nn.xw_plus_b(x1, W1, b_encoder1, name='y1')
Y = tf.compat.v1.nn.xw_plus_b(X, W1, b_encoder1, name='Y')
答案 0 :(得分:0)
请尝试:
b_encoding1 = tf.expand_dims(b_encoding1, axis = 1)
答案 1 :(得分:0)
您可以将X
视为一批x
。 X
可以接受任意数量的样本:
import tensorflow as tf
import numpy as np
X = tf.placeholder(shape=(None, 100), dtype=tf.float32)
W = tf.get_variable('kernel', [100,10])
b = tf.get_variable('bias',[10])
Y = tf.nn.xw_plus_b(X, W,b, name='Y')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer()) # tf version < 1.13
out = sess.run(Y, {X: np.random.rand(128, 100)}) # here n=128
请注意,无论n的值为多少,偏差b
的尺寸仍为10-D。