簡體   English   中英

如何完成以下基於Tensorflow的基於GRU的RNN?

[英]How can I complete following GRU based RNN written in tensorflow?

到目前為止,我已經編寫了以下代碼:

import pickle
import numpy as np
import pandas as pd
import tensorflow as tf

# load pickled objects (x and y)
x_input, y_actual = pickle.load(open('sample_input.pickle', 'rb'))
x_input = np.reshape(x_input, (50, 1))
y_actual = np.reshape(y_actual, (50, 1))

# parameters
batch_size = 50
hidden_size = 100

# create network graph
input_data = tf.placeholder(tf.float32, [batch_size, 1])
output_data = tf.placeholder(tf.float32, [batch_size, 1])

cell = tf.nn.rnn_cell.GRUCell(hidden_size)

initial_state = cell.zero_state(batch_size, tf.float32)

hidden_state = initial_state

output_of_cell, hidden_state = cell(inputs=input_data, state=hidden_state)

init_op = tf.initialize_all_variables()

softmax_w = tf.get_variable("softmax_w", [hidden_size, 1], )
softmax_b = tf.get_variable("softmax_b", [1])

logits = tf.matmul(output_of_cell, softmax_w) + softmax_b

probabilities = tf.nn.softmax(logits)

sess = tf.Session()
sess.run(init_op)

something = sess.run([probabilities, hidden_state], feed_dict={input_data:x_input, output_data:y_actual})

#cost = tf.nn.sigmoid_cross_entropy_with_logits(logits, output_data)


#sess.close()

但是我將s oftmax_w/b作為未初始化變量而報錯。

我不知道如何使用這些Wb進行火車操作。

如下所示:

## some cost function
## training operation minimizing cost function using gradient descent optimizer

tf.initialize_all_variables()從圖中獲取“當前”變量集。 由於在調用tf.initialize_all_variables()之后創建了softmax_wsoftmax_b ,因此它們不在tf.initialize_all_variables()查詢的列表中,因此在運行sess.run(init_op)時不會進行初始化。 以下應該工作:

softmax_w = tf.get_variable("softmax_w", [hidden_size, 1], )
softmax_b = tf.get_variable("softmax_b", [1])

init_op = tf.initialize_all_variables()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM