Char-RNN-TensorFlow icon indicating copy to clipboard operation
Char-RNN-TensorFlow copied to clipboard

在model.py里,sample子函数中,可以将c值赋给x[0,0]么?

Open charmpeng opened this issue 6 years ago • 1 comments

这个代码的主要目的是预测下一个char。

 def sample(self, n_samples, prime, vocab_size):
        samples = [c for c in prime]
        sess = self.session
        new_state = sess.run(self.initial_state)
        preds = np.ones((vocab_size, ))  # for prime=[]
        for c in prime:
            x = np.zeros((1, 1))
            # 输入单个字符
            x[0, 0] = c
            feed = {self.inputs: x,
                    self.keep_prob: 1.,
                    self.initial_state: new_state}
            preds, new_state = sess.run([self.proba_prediction, self.final_state],
                                        feed_dict=feed)

        c = pick_top_n(preds, vocab_size)
        # 添加字符到samples中
        samples.append(c)

但是在这里,直接将c值赋给x[0,0]可以么?x开始初始化是int,但是c是str。我建立了个小程序跑这一段,

import numpy as np
prime = "beauty"
for c in prime:
    x = np.zeros((1,1))
    #x[0,0] = vocab_to_int[c]
    x[0,0] = c
    print("c",c)
    print("x",x)

报错说ValueError: could not convert string to float: 'b'。 请问您是怎么解决这个问题的?

charmpeng avatar Jul 04 '18 01:07 charmpeng

请查看sample函数的调用,prime已经是原始start_string经过 converter.text_to_arr转换成了对应的数字了,是一个数组,不是string

sunnima avatar Mar 19 '19 11:03 sunnima