ImportError:无法导入名称'编码' ,' batch_gen'在Python 3中

时间:2017-06-30 09:48:34

标签: python python-3.5 importerror

您好我无法导入batch_gen,从数据库进行编码。

from data import batch_gen, encode

错误:

ImportError: cannot import name 'batch_gen'

ImportError: cannot import name 'encode'

Python版本:

3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul  5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)]

1 个答案:

答案 0 :(得分:0)

删除此行

from data import batch_gen, encode

相反,添加这两个功能

def encode(X,seq_len, vocab_size):
x = np.zeros((len(X),seq_len, vocab_size), dtype=np.float32)
for ind,batch in enumerate(X):
    for j, elem in enumerate(batch):
        x[ind, j, elem] = 1
return x

def batch_gen(batch_size=32, seq_len=10, max_no=100):
# Randomly generate a batch of integer sequences (X) and its sorted
# counterpart (Y)
x = np.zeros((batch_size, seq_len, max_no), dtype=np.float32)
y = np.zeros((batch_size, seq_len, max_no), dtype=np.float32)

while True:
# Generates a batch of input
    X = np.random.randint(max_no, size=(batch_size, seq_len))

    Y = np.sort(X, axis=1)

    for ind,batch in enumerate(X):
        for j, elem in enumerate(batch):
            x[ind, j, elem] = 1

    for ind,batch in enumerate(Y):
        for j, elem in enumerate(batch):
            y[ind, j, elem] = 1

    yield x, y
    x.fill(0.0)
    y.fill(0.0)