rnn / basic_lstm_cell / kernel已经存在,不允许

时间:2017-10-29 14:26:45

标签: python tensorflow

收到我不知道如何解决的错误。有任何想法吗?形状输入可能有问题吗?

import numpy as np
import tensorflow as tf
from tensorflow.contrib import rnn
import random
import collections
import time

start_time = time.time()

def elapsed(sec):
    if sec<60:
        return str(sec)  + " sec"
    elif sec<(60*60):
        return str(sec/60) + " min"
    else:
        return str(sec/60*60) + " hr"

log_path = '/tmp/tensorflow/rnn_words'
writer = tf.summary.FileWriter(log_path)

training_file = r'C:\Users\danie\Desktop\versuch.txt'

def read_data(file):
    with open (file) as f:
        content = f.readlines()
        content = [x.strip() for x in content]
        content = [content[i].split() for i in range (len(content))]
        content = np.array(content)
        content = np.reshape(content,[-1,])
        return content

training_data = read_data(training_file)

def build_dataset(words):
    count = collections.Counter(words).most_common()
    dictionary = dict()
    for word, _ in count:
        dictionary[word]  = len(dictionary)
    reverse_dictionary  = dict(zip(dictionary.values(), dictionary.keys()))
    return dictionary, reverse_dictionary

dictionary, reverse_dictionary = build_dataset(training_data)
vocab_size = len(dictionary)

learning_rate = 0.001
training_iters = 50000
display_step = 1000
n_input = 3
n_hidden = 512

x = tf.placeholder("float", [None, n_input,1])
y = tf.placeholder("float", [None,vocab_size])

weights = {
    'out': tf.Variable(tf.random_normal([n_hidden, vocab_size]))   
}
biases  = {
    'out': tf.Variable(tf.random_normal([vocab_size]))
}

def RNN(x, weights, biases):
    x = tf.reshape (x,[-1, n_input])
    x = tf.split(x,n_input,1)

    #rnn_cell = rnn.MultiRNNCell([rnn.BasicLSTMCell(n_hidden), rnn.BasicLSTMCell(n_hidden)])
    rnn_cell = rnn.BasicLSTMCell(n_hidden)

    outputs, states = rnn.static_rnn(rnn_cell, x, dtype  = tf.float32)

    return tf.matmul(outputs[-1], weights['out']) + biases['out']

pred = RNN(x, weights, biases)

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels = y))
optimizer  = tf.train.RMSPropOptimizer(learning_rate = learning_rate).minimize(cost)


correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

init  = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(init)
    step = 0
    offset = random.randint(0,n_input+1)
    end_offset = n_input + 1
    acc_total = 0
    loss_total = 0


    writer.add_graph(session.graph)

    while step< training_iters:

        if offset > len(training_data)- end_offset:
            offset = random.randint(0, n_input+1)

        symbols_in_keys = [ [dictionary[ str(training_data[i])]] for i in range(offset, offset+n_input)]
        symbols_in_keys = np.reshape(np.array(symbols_in_keys), [-1, n_input, 1])

        symbols_out_onehot = np.zeros([vocab_size], dtype = float)
        symbols_out_onehot[dictionary[str(training_data[offset+n_input])]]  = 1.0

        _, acc, loss, onehot_pred = session.run([optimizer, accuracy, cost, pred], feed_dict = {x: symbols_in_keys, y: symbols_out_onehot})

        loss_total+= loss

        acc_total+= acc

        if(step+1) % display_step == 0:
            print("Iter= " + str(step + 1) + ", Average Loss= " + "{:.6f}".format(loss_total/display_step) +  ", Average Accuracy= " + "{:.2f}".format(100*acc_total/display_step))
            acc_total = 0
            loss_total = 0
            symbols_in = [training_data[i] for i in range(offset, offset + n_input)]
            symbols_out = training_data[offset + n_input]
            symbolds_out_pred = reverse_dictionary[int(tf.argmax(onehot_pred,1).eval())]

            print("%s - [%s] vs [%s]" % (symbols_in, symbols_out, symbols_out_pred))
        step+= 1
        offset += (n_input+1)
    print("Optimizer finished")
    print('Elapsed Time: ', elapsed(time.time()-start_time))
    print("Run Command line")
    print('')
    print("Point your web browser to: http://localhost:6006/")
    while True:
        promt = "%s words: " % n_input
        sentence = input(prompt)
        sentence = sentence.strip()
        words = sentence.split(' ')
        if len(words) != n_input:
            continue
        try:
            symbols_in_keys = [dictionary[str(words[i])] for i in range(len(words))]
            for i in range(32):
                keys = np.reshape(np.array(symbols_in_keys), [-1, n_input,1])
                onehot_pred = session.run(pred, feed_dict={x: keys})
                onehot_pred_index = int(tf.argmax(onehot_pred,1).eval())
                sentense = "%s %s" (sentence, reverse_dictionary[onehot_pred_index])
                symbols_in_keys = symbols_in_keys[1:]
                symbols_in_keys.append(onehot_pred_index)
            print(sentence)
        except:
            print("Word not in dictionary")

收到我不知道如何解决的错误。有任何想法吗?形状输入可能有问题吗?谢谢 感谢您的回复,我添加了收到的错误消息。希望有所帮助。 编辑:这是错误

    ValueError                                Traceback (most recent call last)
<ipython-input-1-e2e4bba12bcc> in <module>()
    103         symbols_out_onehot[dictionary[str(training_data[offset+n_input])]]  = 1.0
    104 
--> 105         _, acc, loss, onehot_pred = session.run([optimizer, accuracy, cost, pred], feed_dict = {x: symbols_in_keys, y: symbols_out_onehot})
    106 
    107         loss_total+= loss

~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    787     try:
    788       result = self._run(None, fetches, feed_dict, options_ptr,
--> 789                          run_metadata_ptr)
    790       if run_metadata:
    791         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~\Anaconda3\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    973                 'Cannot feed value of shape %r for Tensor %r, '
    974                 'which has shape %r'
--> 975                 % (np_val.shape, subfeed_t.name, str(subfeed_t.get_shape())))
    976           if not self.graph.is_feedable(subfeed_t):
    977             raise ValueError('Tensor %s may not be fed.' % subfeed_t)

ValueError: Cannot feed value of shape (39,) for Tensor 'Placeholder_1:0', which has shape '(?, 39)'

............................................... .................................................. .................................................. .........................................

1 个答案:

答案 0 :(得分:1)

发生错误是因为您的占位符与您的数据不匹配:

y = tf.placeholder("float", [None,vocab_size])
symbols_out_onehot = np.zeros([vocab_size], dtype = float)
feed_dict = {..., y: symbols_out_onehot})

y的排名为[None, vocab_size],排名为2,但symbols_out_onehot的排名为[vocab_size],排名为1,导致错误。

您应该更改占位符ysymbols_out_onehot。通常,将第一个维度作为batch_size是很常见的,因此最好将输入数据更改为:

symbols_out_onehot = np.zeros([1, vocab_size], dtype = float)