Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 我不知道';我不理解与RNN相关的代码_Tensorflow - Fatal编程技术网

Tensorflow 我不知道';我不理解与RNN相关的代码

Tensorflow 我不知道';我不理解与RNN相关的代码,tensorflow,Tensorflow,(请理解英语不是我的母语) 我不明白上面代码的最后一部分“如果,否则”,有人能解释一下吗 为什么仅当j为0时才打印(“”.Join([char_set[t]表示索引中的t]),end=“” 在else的情况下,为什么要打印(char_set[index[-1]],end='') 请解释代码是如何工作的,最后一位只是检查网络是否工作。它首先生成多个结果,然后遍历这些结果。我猜这个代码片段的创建者想要检查第一个结果中的整个句子,然后只检查最后一个字符。这完全取决于你,如果你想改变它的话 最后一位只是

(请理解英语不是我的母语)

我不明白上面代码的最后一部分“如果,否则”,有人能解释一下吗

为什么仅当j为0时才打印(“”.Join([char_set[t]表示索引中的t]),end=“”

在else的情况下,为什么要打印(char_set[index[-1]],end='')


请解释代码是如何工作的,最后一位只是检查网络是否工作。它首先生成多个结果,然后遍历这些结果。我猜这个代码片段的创建者想要检查第一个结果中的整个句子,然后只检查最后一个字符。这完全取决于你,如果你想改变它的话

最后一位只是检查网络是否正常工作。它首先生成多个结果,然后遍历这些结果。我猜这个代码片段的创建者想要检查第一个结果中的整个句子,然后只检查最后一个字符。这完全取决于你,如果你想改变它的话

谢谢你的回答。但是打印(char_set[index[-1]],end='')是如何工作的呢?我不知道索引[-1]是什么意思Spython也允许负索引,它们从后面开始索引。所以[-1]表示数组“index”的最后一个元素。我假设的char_集数组是句子中可能的字符,网络输出这个char_集中的索引(它们包含在数组“index”中)。选择输出的最后一个元素(索引[-1])会在char_集合中给出一个索引,因此您可以通过char_集合[index[-1]]获得句子中的最后一个字符。谢谢您的回答。但是打印(char_set[index[-1]],end='')是如何工作的呢?我不知道索引[-1]是什么意思Spython也允许负索引,它们从后面开始索引。所以[-1]表示数组“index”的最后一个元素。我假设的char_集数组是句子中可能的字符,网络输出这个char_集中的索引(它们包含在数组“index”中)。选择输出的最后一个元素(索引[-1])会在char_集中给出一个索引,因此您可以通过char_集[index[-1]]获得句子中的最后一个字符
from __future__ import print_function

import tensorflow as tf
import numpy as np
from tensorflow.contrib import rnn

tf.set_random_seed(777)  # reproducibility

sentence = ("if you want to build a ship, don't drum up people together to "
            "collect wood and don't assign them tasks and work, but rather "
            "teach them to long for the endless immensity of the sea.")

char_set = list(set(sentence))
char_dic = {w: i for i, w in enumerate(char_set)}

data_dim = len(char_set)
hidden_size = len(char_set)
num_classes = len(char_set)
sequence_length = 10  # Any arbitrary number
learning_rate = 0.1

dataX = []
dataY = []
for i in range(0, len(sentence) - sequence_length):
    x_str = sentence[i:i + sequence_length]
    y_str = sentence[i + 1: i + sequence_length + 1]
    print(i, x_str, '->', y_str)

    x = [char_dic[c] for c in x_str]  # x str to index
    y = [char_dic[c] for c in y_str]  # y str to index

    dataX.append(x)
    dataY.append(y)

batch_size = len(dataX)

X = tf.placeholder(tf.int32, [None, sequence_length])
Y = tf.placeholder(tf.int32, [None, sequence_length])

# One-hot encoding
X_one_hot = tf.one_hot(X, num_classes)
print(X_one_hot)  # check out the shape



def lstm_cell():
    cell = rnn.BasicLSTMCell(hidden_size, state_is_tuple=True)
    return cell

multi_cells = rnn.MultiRNNCell([lstm_cell() for _ in range(2)], state_is_tuple=True)

# outputs: unfolding size x hidden size, state = hidden size
outputs, _states = tf.nn.dynamic_rnn(multi_cells, X_one_hot, dtype=tf.float32)

# FC layer
X_for_fc = tf.reshape(outputs, [-1, hidden_size])
outputs = tf.contrib.layers.fully_connected(X_for_fc, num_classes, activation_fn=None)

# reshape out for sequence_loss
outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes])

# All weights are 1 (equal weights)
weights = tf.ones([batch_size, sequence_length])

sequence_loss = tf.contrib.seq2seq.sequence_loss(
    logits=outputs, targets=Y, weights=weights)
mean_loss = tf.reduce_mean(sequence_loss)
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(mean_loss)

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

for i in range(500):
    _, l, results = sess.run(
        [train_op, mean_loss, outputs], feed_dict={X: dataX, Y: dataY})
    for j, result in enumerate(results):
        index = np.argmax(result, axis=1)
        print(i, j, ''.join([char_set[t] for t in index]), l)

# Let's print the last char of each result to check it works
results = sess.run(outputs, feed_dict={X: dataX})
for j, result in enumerate(results):
    index = np.argmax(result, axis=1)
    if j is 0:  # print all for the first result to make a sentence
        print(''.join([char_set[t] for t in index]), end='')
    else:
        print(char_set[index[-1]], end='')

'''
0 167 tttttttttt 3.23111
0 168 tttttttttt 3.23111
0 169 tttttttttt 3.23111
…
499 167  of the se 0.229616
499 168 tf the sea 0.229616
499 169   the sea. 0.229616

g you want to build a ship, don't drum up people together to collect wood and don't assign them tasks and work, but rather teach them to long for the endless immensity of the sea.

'''