增大a的大小变异系数

2024-05-01 21:40:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在训练一个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)插入(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')

我还声明了损失函数等,然后使用以下内容进行训练:

with tf.compat.v1.Session() as sess:

    sess.run(tf.compat.v1.global_variables_initializer())

    for epoch_i in range(epochs):

        for batch_i in range(number_of_batches):

            batch_data = getBatch(shuffled_data, batch_i, batch_size)
            sess.run(optimizer, feed_dict={x1: batch_data[:,:,0], X: batch_data})

        train_loss = sess.run(loss, feed_dict={x1: aug_data[:,:,0], X: aug_data})
        print(epoch_i, train_loss)

Tags: nameinputdatatfbatchcodelengthw1
2条回答

您可以将X视为一批xX可以接收任意数量的样本:

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

请尝试:

b_encoding1 = tf.expand_dims(b_encoding1, axis = 1)

相关问题 更多 >