Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
charmpeng opened this issue Jul 4, 2018 · 1 comment

Comments

@charmpeng
Copy link

这个代码的主要目的是预测下一个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'。
请问您是怎么解决这个问题的?

@sunnima
Copy link

sunnima commented Mar 19, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants