【问题标题】:accessing the values from a Tensor Variable of Tensorflow从 Tensorflow 的张量变量中访问值
【发布时间】:2017-01-18 05:25:06
【问题描述】:

我已经修改了自动编码器的示例代码以适应我的数据。我想知道一种访问编码器 layer_1 值的方法。 我想使用编码器编码值作为输入数据的特征并对此进行进一步分析。 输入为 200 维,编码器有 100 个隐藏节点。

from __future__ import division, print_function, absolute_import

import tensorflow as tf
import numpy as np
import word2vec

# Parameters
learning_rate = 0.01
training_epochs = 200

#layer_1 = None
#layer_2 = None

# Network Parameters
n_hidden_1 = 100 # 1st layer num features
n_input = 200 # concatenation of 2 word vectors

X = tf.placeholder("float", [None, n_input])

weights = {
    'encoder_h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
    'decoder_h1': tf.Variable(tf.random_normal([n_hidden_1, n_input])),
}
biases = {
    'encoder_b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'decoder_b1': tf.Variable(tf.random_normal([n_input])),
}

# Building the encoder
def encoder(x):
    #global layer_1
    layer_1 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['encoder_h1']),
                               biases['encoder_b1']))
    return layer_1

# Building the decoder
def decoder(x):
    #global layer_2
    layer_2 = tf.nn.sigmoid(tf.add(tf.matmul(x, weights['decoder_h1']),
                               biases['decoder_b1']))
    return layer_2


 # Construct model
encoder_op = encoder(X)
decoder_op = decoder(encoder_op)

# Prediction
y_pred = decoder_op
# Targets (Labels) are the input data.
y_true = X


# Define loss and optimizer, minimize the squared error
cost = tf.reduce_mean(tf.pow(y_true - y_pred, 2))
optimizer = tf.train.RMSPropOptimizer(learning_rate).minimize(cost)

# Initializing the variables
init = tf.initialize_all_variables()

with tf.Session() as sess:
    sess.run(init)    
    model = word2vec.load('./vectors.bin')    
    vector1 = (list(model['word1']))
    vector2 = (list(model['word2']))    
    input = []
    input.append(vector1+vector2)
    input_np = np.array(input)  
    for epoch in range(training_epochs):
        #import pdb; pdb.set_trace()
        _, c = sess.run([optimizer, cost], feed_dict={X: input_np}) 

    print(sess.run(w))

【问题讨论】:

  • sess.run(encoder_op) ?
  • @martianwars sess.run(encoder_op) .. 我收到一个错误您必须使用 dtype float [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]] [[节点:Sigmoid/_217 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0 /task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_13_Sigmoid", tensor_type=DT_FLOAT, _device="/job :localhost/replica:0/task:0/cpu:0"]()]]
  • 那么在 epoch 循环之后运行这一行?
  • 我仍然遇到同样的错误
  • 你能分享你最新的代码吗?

标签: tensorflow tensorboard


【解决方案1】:

为了查看 TensorFlow 计算中发生的情况,我建议使用 TensorBoard。 TensorBoard 包含Summary Operations,然后可以在 UI 中绘制,以将张量的值可视化为模型训练。

对于高级别的介绍,我强烈推荐 TensorFlow 开发者峰会的这个 YouTube 视频:https://www.youtube.com/watch?v=eBbEDRsCmv4

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多