2016-06-01 11 views
0

を実行し、次の例で考えてみましょう:私はその中間を維持するために、各反復でゆっくりと変化するテンソルstateを持つように期待していた保存テンソル値

import tensorflow as tf 
import math 
import numpy as np 

INPUTS = 10 
HIDDEN_1 = 20 
BATCH_SIZE = 3 


def create_graph(inputs): 
    with tf.name_scope('h1'): 
     weights = tf.Variable(
     tf.truncated_normal([INPUTS, HIDDEN_1], 
          stddev=1.0/math.sqrt(float(INPUTS))), 
     name='weights') 
     biases = tf.Variable(tf.zeros([HIDDEN_1]), 
         name='biases') 
     state = tf.Variable(tf.zeros([HIDDEN_1]), name='inner_state') 
     state = tf.Print(state, [state], message=" this is state before: ") 
     state = 0.9*state + 0.1*(tf.matmul(inputs, weights) + biases) 
     state = tf.Print(state, [state], message=" this is state after: ") 
     output = tf.nn.relu(state) 
    return output 

def data_iter(): 
    while True: 
     idxs = np.random.rand(BATCH_SIZE, INPUTS) 
     yield idxs 

with tf.Graph().as_default(): 
    inputs = tf.placeholder(tf.float32, shape=(BATCH_SIZE, INPUTS)) 
    output = create_graph(inputs) 

    sess = tf.Session() 
    # Run the Op to initialize the variables. 
    init = tf.initialize_all_variables() 
    sess.run(init) 
    iter_ = data_iter() 
    for i in xrange(0, 2): 
     print ("iteration: ",i) 
     input_data = iter_.next() 
     out = sess.run(output, feed_dict={ inputs: input_data}) 

を。しかし、私が見ていて、各sess.run呼び出しで、状態はゼロ値に開始できることである:

('iteration: ', 0) 
I tensorflow/core/kernels/logging_ops.cc:79] this is state before: [0 0 0...] 
I tensorflow/core/kernels/logging_ops.cc:79] this is state after: [0.007762237 0.044753391 0.049343754...] 
('iteration: ', 1) 
I tensorflow/core/kernels/logging_ops.cc:79] this is state before: [0 0 0...] 
I tensorflow/core/kernels/logging_ops.cc:79] this is state after: [0.040079735 0.074709542 0.078258425...] 

私はこの振る舞い


編集

に対処する方法を任意の明確化をお願い申し上げます

の行をtf.Print行に書き換えて間違った割り当てを置き換えた後

state = state.assign(0.9*state + 0.1*(tf.matmul(inputs, weights) + biases)) 

私はこれらのエラーを取得する:あなたがstate = 0.9 * state + 0.1 * (tf.matmul(inputs, weights) + biases)を書くとき、あなたは変数stateの値を

Traceback (most recent call last): 
    File "cycles_in_graphs.py", line 33, in <module> 
    output = create_graph(inputs) 
    File "cycles_in_graphs.py", line 21, in create_graph 
    state = state.assign(0.9*state + 0.1*(tf.matmul(inputs, weights) + biases)) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variables.py", line 453, in assign 
    return state_ops.assign(self._variable, value, use_locking=use_locking) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_state_ops.py", line 40, in assign 
    use_locking=use_locking, name=name) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/op_def_library.py", line 655, in apply_op 
    op_def=op_def) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2156, in create_op 
    set_shapes_for_outputs(ret) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1612, in set_shapes_for_outputs 
    shapes = shape_func(op) 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/state_ops.py", line 197, in _AssignShape 
    return [op.inputs[0].get_shape().merge_with(op.inputs[1].get_shape())] 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 554, in merge_with 
    (self, other)) 
ValueError: Shapes (20,) and (3, 20) are not compatible 

答えて

1

更新されません。
の値を0.9 * state + 0.1 * ...と計算しますが、変数の値は変わりません。


あなたtf.Variableを更新するには、あなたはあなたの変数stateに機能assignまたはassign_addを使用する必要があります。

state = state.assign(0.9 * state + 0.1 * (tf.matmul(inputs, weights) + biases)) 

すべてがTensorFlow tutorial on Variablesに説明されています。

+0

ありがとうございました。 AttributeError: 'Tensor'オブジェクトに 'assign'属性がありません。 " – diffeomorphism

+0

はい、このエラーが発生しました。「状態= state.assign(0.9 *状態+ 0.1 *(tf.matmul(入力、重み)+バイアス)直前に 'state = tf.Print(state)'を実行します。 'tf.Variable'だけが属性' assign() 'を持っています –

+0

ありがとう!私は代入式を更新しましたが、いくつかの互換性のない形状エラーが発生しました。私は誤差を伴うOPを更新しました – diffeomorphism