官网实例详解4.18(lstm_seq2seq.py)-keras学习笔记四


基于keras(实现)序列到序列的例子(字符级)。

     英文翻译为法文的实例

准备

下载fra-eng并解压到和py文件同目录

fra-eng目录文件


fra.txt(corpus,语料库)文件内容,每行 英文单词+空格+法文单词


Keras实例目录

代码注释

'''Sequence to sequence example in Keras (character-level).
基于keras(实现)序列到序列的例子(字符级)。
This script demonstrates how to implement a basic character-level
sequence-to-sequence model. We apply it to translating
short English sentences into short French sentences,
character-by-character. Note that it is fairly unusual to
do character-level machine translation, as word-level
models are more common in this domain.
本脚本演示如何实现一个基本的字符级序列到序列模型。我们将它应用于英语短句到法语的翻译。
注意,字符级机器翻译是比较少,而单词级模型在这一领域更为常见。

# Summary of the algorithm
算法综述

- We start with input sequences from a domain (e.g. English sentences)
    and correspding target sequences from another domain
    (e.g. French sentences).
    从一个范围的输入序列(例如英语句子)和来自另一个范围(例如法语句子)的目标序列开始。
- An encoder LSTM turns input sequences to 2 state vectors
    (we keep the last LSTM state and discard the outputs).
    编码器LSTM将输入序列转换为2个状态向量(我们保持最后一个LSTM状态并丢弃输出)。
- A decoder LSTM is trained to turn the target sequences into
    the same sequence but offset by one timestep in the future,
    a training process called "teacher forcing" in this context.
    Is uses as initial state the state vectors from the encoder.
    Effectively, the decoder learns to generate `targets[t+1...]`
    given `targets[...t]`, conditioned on the input sequence.
- 解码器LSTM被训练成将目标序列转换成相同的序列,但是在将来被一个时间步长偏移,
在此上下文中称为“teacher forcing”的训练过程。作为编码器的初始状态,有效地,译码器
学会在输入序列条件下生成“目标[t+1…] ]给定的‘目标[…t]’。
- In inference mode, when we want to decode unknown input sequences, we:
- 在推理模式中,当我们想要解码未知输入序列时,我们:
    - Encode the input sequence into state vectors
    - 将输入序列编码为状态向量
    - Start with a target sequence of size 1
        (just the start-of-sequence character)
    - 从大小为1的目标序列开始(只是序列字符的开始)
    - Feed the state vectors and 1-char target sequence
        to the decoder to produce predictions for the next character
    - 将状态向量和1-Char目标序列送到解码器以生成下一个字符的预测。
    - Sample the next character using these predictions
        (we simply use argmax).
    - 使用这些预测来采样下一个字符(我们只使用argmax)。
    - Append the sampled character to the target sequence
    - 采样的字符追加到目标序列
    - Repeat until we generate the end-of-sequence character or we
        hit the character limit.
    - 重复,直到产生序列结束字符或到达字符限制。

# Data download
数据下载
English to French sentence pairs.
英语到法语句子对
http://www.manythings.org/anki/fra-eng.zip

Lots of neat sentence pairs datasets can be found at:
大量整齐的句子对数据集可以在以下网址发现:
http://www.manythings.org/anki/

# References
参考
- Sequence to Sequence Learning with Neural Networks
使用神经网络进行序列到序列学习
    https://arxiv.org/abs/1409.3215
- Learning Phrase Representations using
学习短语表示
    RNN Encoder-Decoder for Statistical Machine Translation
    用于统计机器翻译的RNN(循环神经网络)编码器解码器
    https://arxiv.org/abs/1406.1078
'''
from __future__ import print_function

from keras.models import Model
from keras.layers import Input, LSTM, Dense
import numpy as np

batch_size = 64  # Batch size for training. 训练批次大小(每个批次包含样本数)
epochs = 100  # Number of epochs to train for.训练周期数
latent_dim = 256  # Latent dimensionality of the encoding space.编码空间的潜在维数
num_samples = 10000  # Number of samples to train on.训练集样本数
# Path to the data txt file on disk.
# 存储器数据文件路径(先下载,然后存放到和本脚本同级目录)
data_path = 'fra-eng/fra.txt'

# Vectorize the data.
# 数据向量化
input_texts = []
target_texts = []
input_characters = set()
target_characters = set()
with open(data_path, 'r', encoding='utf-8') as f:
    lines = f.read().split('\n')
for line in lines[: min(num_samples, len(lines) - 1)]:
    input_text, target_text = line.split('\t')
    # We use "tab" as the "start sequence" character
    # 使用"tab"为开始序列字符
    # for the targets, and "\n" as "end sequence" character.
    # 使用“tab”作为目标的“开始序列”字符,而“\n”作为“结束序列”字符。
    target_text = '\t' + target_text + '\n'
    input_texts.append(input_text)
    target_texts.append(target_text)
    for char in input_text:
        if char not in input_characters:
            input_characters.add(char)
    for char in target_text:
        if char not in target_characters:
            target_characters.add(char)

input_characters = sorted(list(input_characters))
target_characters = sorted(list(target_characters))
num_encoder_tokens = len(input_characters)
num_decoder_tokens = len(target_characters)
max_encoder_seq_length = max([len(txt) for txt in input_texts])
max_decoder_seq_length = max([len(txt) for txt in target_texts])

print('Number of samples:', len(input_texts))
print('Number of unique input tokens:', num_encoder_tokens)
print('Number of unique output tokens:', num_decoder_tokens)
print('Max sequence length for inputs:', max_encoder_seq_length)
print('Max sequence length for outputs:', max_decoder_seq_length)

input_token_index = dict(
    [(char, i) for i, char in enumerate(input_characters)])
target_token_index = dict(
    [(char, i) for i, char in enumerate(target_characters)])

encoder_input_data = np.zeros(
    (len(input_texts), max_encoder_seq_length, num_encoder_tokens),
    dtype='float32')
decoder_input_data = np.zeros(
    (len(input_texts), max_decoder_seq_length, num_decoder_tokens),
    dtype='float32')
decoder_target_data = np.zeros(
    (len(input_texts), max_decoder_seq_length, num_decoder_tokens),
    dtype='float32')

for i, (input_text, target_text) in enumerate(zip(input_texts, target_texts)):
    for t, char in enumerate(input_text):
        encoder_input_data[i, t, input_token_index[char]] = 1.
    for t, char in enumerate(target_text):
        # decoder_target_data is ahead of decoder_input_data by one timestep
        # decoder_target_data领先decoder_input_data一步
        decoder_input_data[i, t, target_token_index[char]] = 1.
        if t > 0:
            # decoder_target_data will be ahead by one timestep
            # and will not include the start character.
            # decoder_target_data数据将提前一个时间步,不包括开始字符。
            decoder_target_data[i, t - 1, target_token_index[char]] = 1.

# Define an input sequence and process it.
# 定义一个输入序列并对其进行处理。
encoder_inputs = Input(shape=(None, num_encoder_tokens))
encoder = LSTM(latent_dim, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
# We discard `encoder_outputs` and only keep the states.
# 丢弃“encoder_outputs”,只保留状态。
encoder_states = [state_h, state_c]

# Set up the decoder, using `encoder_states` as initial state.
# 设置解码器,使用encoder_states作为初始状态。
decoder_inputs = Input(shape=(None, num_decoder_tokens))
# We set up our decoder to return full output sequences,
# and to return internal states as well. We don't use the
# return states in the training model, but we will use them in inference.
# 设置解码器返回完整的输出序列,并返回内部状态。我们在训练模型中不使用返回状态,但是我们将在推理中使用它们。
decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True)
decoder_outputs, _, _ = decoder_lstm(decoder_inputs,
                                     initial_state=encoder_states)
decoder_dense = Dense(num_decoder_tokens, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)

# Define the model that will turn
# `encoder_input_data` & `decoder_input_data` into `decoder_target_data`
# 定义将encoder_input_data和decoder_input_data转换为decoder_target_data的模型
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)

# Run training
# 运行模型
model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
          batch_size=batch_size,
          epochs=epochs,
          validation_split=0.2)
# Save model
# 保存模型
model.save('s2s.h5')

# Next: inference mode (sampling).
# 下一步:推理模式(采样)。
# Here's the drill:
# 练习:
# 1) encode input and retrieve initial decoder state
# 1) 编码输入和检索初始解码器状态
# 2) run one step of decoder with this initial state
# and a "start of sequence" token as target.
# Output will be the next target token
# 2) 以这个初始状态和一个“序列开始”分词为目标运行一步解码器。输出将是下一个目标分词
# 3) Repeat with the current target token and current states
# 3) 重复当前目标分词和当前状态

# Define sampling models
# 定义抽样模型
encoder_model = Model(encoder_inputs, encoder_states)

decoder_state_input_h = Input(shape=(latent_dim,))
decoder_state_input_c = Input(shape=(latent_dim,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm(
    decoder_inputs, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model(
    [decoder_inputs] + decoder_states_inputs,
    [decoder_outputs] + decoder_states)

# Reverse-lookup token index to decode sequences back to
# something readable.
# 反向查找分词索引(词的编号)将序列解码为可读的。
reverse_input_char_index = dict(
    (i, char) for char, i in input_token_index.items())
reverse_target_char_index = dict(
    (i, char) for char, i in target_token_index.items())


def decode_sequence(input_seq):
    # Encode the input as state vectors.
    # 将输入(数据)编码为状态向量。
    states_value = encoder_model.predict(input_seq)

    # Generate empty target sequence of length 1.
    # 生成长度为1的空目标序列。
    target_seq = np.zeros((1, 1, num_decoder_tokens))
    # Populate the first character of target sequence with the start character.
    # 用开始字符填充目标序列的第一个字符。
    target_seq[0, 0, target_token_index['\t']] = 1.

    # Sampling loop for a batch of sequences
    # (to simplify, here we assume a batch of size 1).
    # 一批序列的采样循环(为了简化,这里假设一批次大小为1)。
    stop_condition = False
    decoded_sentence = ''
    while not stop_condition:
        output_tokens, h, c = decoder_model.predict(
            [target_seq] + states_value)

        # Sample a token
        # 采样
        sampled_token_index = np.argmax(output_tokens[0, -1, :])
        sampled_char = reverse_target_char_index[sampled_token_index]
        decoded_sentence += sampled_char

        # Exit condition: either hit max length
        # or find stop character.
        # 退出条件:到达最大长度或找到停止字符。
        if (sampled_char == '\n' or
           len(decoded_sentence) > max_decoder_seq_length):
            stop_condition = True

        # Update the target sequence (of length 1).
        # 更新目标序列(长度1)。
        target_seq = np.zeros((1, 1, num_decoder_tokens))
        target_seq[0, 0, sampled_token_index] = 1.

        # Update states
        # 更新状态
        states_value = [h, c]

    return decoded_sentence


for seq_index in range(100):
    # Take one sequence (part of the training set)
    # for trying out decoding.
    # 用一个序列(训练集的一部分)进行解码。
    input_seq = encoder_input_data[seq_index: seq_index + 1]
    decoded_sentence = decode_sequence(input_seq)
    print('-')
    print('Input sentence:', input_texts[seq_index])
    print('Decoded sentence:', decoded_sentence)

代码执行

C:\ProgramData\Anaconda3\python.exe E:/keras-master/examples/lstm_seq2seq.py
Using TensorFlow backend.
Number of samples: 10000
Number of unique input tokens: 71
Number of unique output tokens: 94
Max sequence length for inputs: 16
Max sequence length for outputs: 59
Train on 8000 samples, validate on 2000 samples
Epoch 1/100

  64/8000 [..............................] - ETA: 3:58 - loss: 1.3394
 128/8000 [..............................] - ETA: 2:07 - loss: 1.3604
 192/8000 [..............................] - ETA: 1:30 - loss: 1.3536
 256/8000 [..............................] - ETA: 1:11 - loss: 1.3082
 320/8000 [>.............................] - ETA: 1:00 - loss: 1.2752
 384/8000 [>.............................] - ETA: 52s - loss: 1.2475 
 448/8000 [>.............................] - ETA: 49s - loss: 1.2114
 512/8000 [>.............................] - ETA: 45s - loss: 1.1931
 576/8000 [=>............................] - ETA: 42s - loss: 1.1733
 640/8000 [=>............................] - ETA: 40s - loss: 1.1549
 704/8000 [=>............................] - ETA: 38s - loss: 1.1414
 768/8000 [=>............................] - ETA: 36s - loss: 1.1241
 832/8000 [==>...........................] - ETA: 34s - loss: 1.1123
 896/8000 [==>...........................] - ETA: 33s - loss: 1.0999
 960/8000 [==>...........................] - ETA: 32s - loss: 1.0876
1024/8000 [==>...........................] - ETA: 31s - loss: 1.0807
1088/8000 [===>..........................] - ETA: 29s - loss: 1.0802
1152/8000 [===>..........................] - ETA: 29s - loss: 1.0724
1216/8000 [===>..........................] - ETA: 28s - loss: 1.0677
1280/8000 [===>..........................] - ETA: 27s - loss: 1.0614
1344/8000 [====>.........................] - ETA: 26s - loss: 1.0563
1408/8000 [====>.........................] - ETA: 25s - loss: 1.0538
1472/8000 [====>.........................] - ETA: 25s - loss: 1.0513
1536/8000 [====>.........................] - ETA: 24s - loss: 1.0495
1600/8000 [=====>........................] - ETA: 24s - loss: 1.0470
1664/8000 [=====>........................] - ETA: 23s - loss: 1.0434
1728/8000 [=====>........................] - ETA: 23s - loss: 1.0394
1792/8000 [=====>........................] - ETA: 22s - loss: 1.0357
1856/8000 [=====>........................] - ETA: 22s - loss: 1.0331
1920/8000 [======>.......................] - ETA: 21s - loss: 1.0277
1984/8000 [======>.......................] - ETA: 21s - loss: 1.0275
2048/8000 [======>.......................] - ETA: 20s - loss: 1.0253
2112/8000 [======>.......................] - ETA: 20s - loss: 1.0230
2176/8000 [=======>......................] - ETA: 19s - loss: 1.0212
2240/8000 [=======>......................] - ETA: 19s - loss: 1.0179
2304/8000 [=======>......................] - ETA: 19s - loss: 1.0159
2368/8000 [=======>......................] - ETA: 18s - loss: 1.0129
2432/8000 [========>.....................] - ETA: 18s - loss: 1.0099
2496/8000 [========>.....................] - ETA: 18s - loss: 1.0090
2560/8000 [========>.....................] - ETA: 17s - loss: 1.0056
2624/8000 [========>.....................] - ETA: 17s - loss: 1.0041
2688/8000 [=========>....................] - ETA: 17s - loss: 1.0007
2752/8000 [=========>....................] - ETA: 17s - loss: 0.9998
2816/8000 [=========>....................] - ETA: 16s - loss: 0.9978
2880/8000 [=========>....................] - ETA: 16s - loss: 0.9955
2944/8000 [==========>...................] - ETA: 16s - loss: 0.9933
3008/8000 [==========>...................] - ETA: 16s - loss: 0.9921
3072/8000 [==========>...................] - ETA: 16s - loss: 0.9906
3136/8000 [==========>...................] - ETA: 15s - loss: 0.9873
3200/8000 [===========>..................] - ETA: 15s - loss: 0.9864
3264/8000 [===========>..................] - ETA: 15s - loss: 0.9858
3328/8000 [===========>..................] - ETA: 15s - loss: 0.9846
3392/8000 [===========>..................] - ETA: 15s - loss: 0.9833
3456/8000 [===========>..................] - ETA: 14s - loss: 0.9822
3520/8000 [============>.................] - ETA: 14s - loss: 0.9816
3584/8000 [============>.................] - ETA: 14s - loss: 0.9807
3648/8000 [============>.................] - ETA: 14s - loss: 0.9810
3712/8000 [============>.................] - ETA: 13s - loss: 0.9794
3776/8000 [=============>................] - ETA: 13s - loss: 0.9782
3840/8000 [=============>................] - ETA: 13s - loss: 0.9768
3904/8000 [=============>................] - ETA: 13s - loss: 0.9757
3968/8000 [=============>................] - ETA: 12s - loss: 0.9745
4032/8000 [==============>...............] - ETA: 12s - loss: 0.9737
4096/8000 [==============>...............] - ETA: 12s - loss: 0.9729
4160/8000 [==============>...............] - ETA: 12s - loss: 0.9711
4224/8000 [==============>...............] - ETA: 11s - loss: 0.9697
4288/8000 [===============>..............] - ETA: 11s - loss: 0.9685
4352/8000 [===============>..............] - ETA: 11s - loss: 0.9677
4416/8000 [===============>..............] - ETA: 11s - loss: 0.9667
4480/8000 [===============>..............] - ETA: 10s - loss: 0.9656
4544/8000 [================>.............] - ETA: 10s - loss: 0.9646
4608/8000 [================>.............] - ETA: 10s - loss: 0.9636
4672/8000 [================>.............] - ETA: 10s - loss: 0.9623
4736/8000 [================>.............] - ETA: 10s - loss: 0.9612
4800/8000 [=================>............] - ETA: 9s - loss: 0.9606 
4864/8000 [=================>............] - ETA: 9s - loss: 0.9600
4928/8000 [=================>............] - ETA: 9s - loss: 0.9587
4992/8000 [=================>............] - ETA: 9s - loss: 0.9586
5056/8000 [=================>............] - ETA: 9s - loss: 0.9581
5120/8000 [==================>...........] - ETA: 8s - loss: 0.9571
5184/8000 [==================>...........] - ETA: 8s - loss: 0.9561
5248/8000 [==================>...........] - ETA: 8s - loss: 0.9559
5312/8000 [==================>...........] - ETA: 8s - loss: 0.9554
5376/8000 [===================>..........] - ETA: 7s - loss: 0.9539
5440/8000 [===================>..........] - ETA: 7s - loss: 0.9532
5504/8000 [===================>..........] - ETA: 7s - loss: 0.9524
5568/8000 [===================>..........] - ETA: 7s - loss: 0.9521
5632/8000 [====================>.........] - ETA: 7s - loss: 0.9511
5696/8000 [====================>.........] - ETA: 6s - loss: 0.9501
5760/8000 [====================>.........] - ETA: 6s - loss: 0.9493
5824/8000 [====================>.........] - ETA: 6s - loss: 0.9488
5888/8000 [=====================>........] - ETA: 6s - loss: 0.9482
5952/8000 [=====================>........] - ETA: 6s - loss: 0.9476
6016/8000 [=====================>........] - ETA: 5s - loss: 0.9470
6080/8000 [=====================>........] - ETA: 5s - loss: 0.9459
6144/8000 [======================>.......] - ETA: 5s - loss: 0.9455
6208/8000 [======================>.......] - ETA: 5s - loss: 0.9448
6272/8000 [======================>.......] - ETA: 5s - loss: 0.9445
6336/8000 [======================>.......] - ETA: 4s - loss: 0.9440
6400/8000 [=======================>......] - ETA: 4s - loss: 0.9434
6464/8000 [=======================>......] - ETA: 4s - loss: 0.9425
6528/8000 [=======================>......] - ETA: 4s - loss: 0.9420
6592/8000 [=======================>......] - ETA: 4s - loss: 0.9414
6656/8000 [=======================>......] - ETA: 3s - loss: 0.9411
6720/8000 [========================>.....] - ETA: 3s - loss: 0.9401
6784/8000 [========================>.....] - ETA: 3s - loss: 0.9392
6848/8000 [========================>.....] - ETA: 3s - loss: 0.9385
6912/8000 [========================>.....] - ETA: 3s - loss: 0.9374
6976/8000 [=========================>....] - ETA: 2s - loss: 0.9369
7040/8000 [=========================>....] - ETA: 2s - loss: 0.9361
7104/8000 [=========================>....] - ETA: 2s - loss: 0.9352
7168/8000 [=========================>....] - ETA: 2s - loss: 0.9345
7232/8000 [==========================>...] - ETA: 2s - loss: 0.9340
7296/8000 [==========================>...] - ETA: 2s - loss: 0.9337
7360/8000 [==========================>...] - ETA: 1s - loss: 0.9332
7424/8000 [==========================>...] - ETA: 1s - loss: 0.9328
7488/8000 [===========================>..] - ETA: 1s - loss: 0.9323
7552/8000 [===========================>..] - ETA: 1s - loss: 0.9319
7616/8000 [===========================>..] - ETA: 1s - loss: 0.9314
7680/8000 [===========================>..] - ETA: 0s - loss: 0.9311
7744/8000 [============================>.] - ETA: 0s - loss: 0.9304
7808/8000 [============================>.] - ETA: 0s - loss: 0.9295
7872/8000 [============================>.] - ETA: 0s - loss: 0.9289
7936/8000 [============================>.] - ETA: 0s - loss: 0.9284
8000/8000 [==============================] - 25s 3ms/step - loss: 0.9274 - val_loss: 1.0000
Epoch 2/100

  64/8000 [..............................] - ETA: 17s - loss: 0.8085
 128/8000 [..............................] - ETA: 22s - loss: 0.8087
 192/8000 [..............................] - ETA: 23s - loss: 0.8104
 

Epoch 100/100

  64/8000 [..............................] - ETA: 15s - loss: 0.0462
 128/8000 [..............................] - ETA: 17s - loss: 0.0495
 192/8000 [..............................] - ETA: 17s - loss: 0.0503
 256/8000 [..............................] - ETA: 16s - loss: 0.0533
 320/8000 [>.............................] - ETA: 17s - loss: 0.0527
 384/8000 [>.............................] - ETA: 17s - loss: 0.0525
 448/8000 [>.............................] - ETA: 17s - loss: 0.0532
 512/8000 [>.............................] - ETA: 16s - loss: 0.0526
 576/8000 [=>............................] - ETA: 16s - loss: 0.0530
 640/8000 [=>............................] - ETA: 16s - loss: 0.0530
 704/8000 [=>............................] - ETA: 16s - loss: 0.0529
 768/8000 [=>............................] - ETA: 16s - loss: 0.0533
 832/8000 [==>...........................] - ETA: 16s - loss: 0.0532
 896/8000 [==>...........................] - ETA: 16s - loss: 0.0537
 960/8000 [==>...........................] - ETA: 15s - loss: 0.0535
1024/8000 [==>...........................] - ETA: 15s - loss: 0.0532
1088/8000 [===>..........................] - ETA: 15s - loss: 0.0532
1152/8000 [===>..........................] - ETA: 15s - loss: 0.0531
1216/8000 [===>..........................] - ETA: 15s - loss: 0.0530
1280/8000 [===>..........................] - ETA: 14s - loss: 0.0535
1344/8000 [====>.........................] - ETA: 14s - loss: 0.0536
1408/8000 [====>.........................] - ETA: 14s - loss: 0.0535
1472/8000 [====>.........................] - ETA: 14s - loss: 0.0534
1536/8000 [====>.........................] - ETA: 14s - loss: 0.0533
1600/8000 [=====>........................] - ETA: 14s - loss: 0.0533
1664/8000 [=====>........................] - ETA: 14s - loss: 0.0535
1728/8000 [=====>........................] - ETA: 13s - loss: 0.0536
1792/8000 [=====>........................] - ETA: 13s - loss: 0.0535
1856/8000 [=====>........................] - ETA: 13s - loss: 0.0538
1920/8000 [======>.......................] - ETA: 13s - loss: 0.0539
1984/8000 [======>.......................] - ETA: 13s - loss: 0.0542
2048/8000 [======>.......................] - ETA: 13s - loss: 0.0543
2112/8000 [======>.......................] - ETA: 13s - loss: 0.0543
2176/8000 [=======>......................] - ETA: 13s - loss: 0.0545
2240/8000 [=======>......................] - ETA: 12s - loss: 0.0545
2304/8000 [=======>......................] - ETA: 12s - loss: 0.0546
2368/8000 [=======>......................] - ETA: 12s - loss: 0.0546
2432/8000 [========>.....................] - ETA: 12s - loss: 0.0545
2496/8000 [========>.....................] - ETA: 12s - loss: 0.0546
2560/8000 [========>.....................] - ETA: 12s - loss: 0.0547
2624/8000 [========>.....................] - ETA: 12s - loss: 0.0547
2688/8000 [=========>....................] - ETA: 11s - loss: 0.0549
2752/8000 [=========>....................] - ETA: 11s - loss: 0.0549
2816/8000 [=========>....................] - ETA: 11s - loss: 0.0550
2880/8000 [=========>....................] - ETA: 11s - loss: 0.0550
2944/8000 [==========>...................] - ETA: 11s - loss: 0.0549
3008/8000 [==========>...................] - ETA: 11s - loss: 0.0550
3072/8000 [==========>...................] - ETA: 11s - loss: 0.0551
3136/8000 [==========>...................] - ETA: 10s - loss: 0.0552
3200/8000 [===========>..................] - ETA: 10s - loss: 0.0554
3264/8000 [===========>..................] - ETA: 10s - loss: 0.0555
3328/8000 [===========>..................] - ETA: 10s - loss: 0.0554
3392/8000 [===========>..................] - ETA: 10s - loss: 0.0555
3456/8000 [===========>..................] - ETA: 10s - loss: 0.0555
3520/8000 [============>.................] - ETA: 9s - loss: 0.0556 
3584/8000 [============>.................] - ETA: 9s - loss: 0.0557
3648/8000 [============>.................] - ETA: 9s - loss: 0.0558
3712/8000 [============>.................] - ETA: 9s - loss: 0.0557
3776/8000 [=============>................] - ETA: 9s - loss: 0.0557
3840/8000 [=============>................] - ETA: 9s - loss: 0.0558
3904/8000 [=============>................] - ETA: 9s - loss: 0.0558
3968/8000 [=============>................] - ETA: 8s - loss: 0.0559
4032/8000 [==============>...............] - ETA: 8s - loss: 0.0559
4096/8000 [==============>...............] - ETA: 8s - loss: 0.0560
4160/8000 [==============>...............] - ETA: 8s - loss: 0.0561
4224/8000 [==============>...............] - ETA: 8s - loss: 0.0561
4288/8000 [===============>..............] - ETA: 8s - loss: 0.0561
4352/8000 [===============>..............] - ETA: 8s - loss: 0.0561
4416/8000 [===============>..............] - ETA: 8s - loss: 0.0562
4480/8000 [===============>..............] - ETA: 7s - loss: 0.0563
4544/8000 [================>.............] - ETA: 7s - loss: 0.0564
4608/8000 [================>.............] - ETA: 7s - loss: 0.0564
4672/8000 [================>.............] - ETA: 7s - loss: 0.0566
4736/8000 [================>.............] - ETA: 7s - loss: 0.0566
4800/8000 [=================>............] - ETA: 7s - loss: 0.0567
4864/8000 [=================>............] - ETA: 7s - loss: 0.0568
4928/8000 [=================>............] - ETA: 6s - loss: 0.0568
4992/8000 [=================>............] - ETA: 6s - loss: 0.0569
5056/8000 [=================>............] - ETA: 6s - loss: 0.0569
5120/8000 [==================>...........] - ETA: 6s - loss: 0.0571
5184/8000 [==================>...........] - ETA: 6s - loss: 0.0570
5248/8000 [==================>...........] - ETA: 6s - loss: 0.0571
5312/8000 [==================>...........] - ETA: 6s - loss: 0.0572
5376/8000 [===================>..........] - ETA: 5s - loss: 0.0573
5440/8000 [===================>..........] - ETA: 5s - loss: 0.0574
5504/8000 [===================>..........] - ETA: 5s - loss: 0.0574
5568/8000 [===================>..........] - ETA: 5s - loss: 0.0575
5632/8000 [====================>.........] - ETA: 5s - loss: 0.0575
5696/8000 [====================>.........] - ETA: 5s - loss: 0.0576
5760/8000 [====================>.........] - ETA: 5s - loss: 0.0576
5824/8000 [====================>.........] - ETA: 4s - loss: 0.0576
5888/8000 [=====================>........] - ETA: 4s - loss: 0.0577
5952/8000 [=====================>........] - ETA: 4s - loss: 0.0578
6016/8000 [=====================>........] - ETA: 4s - loss: 0.0578
6080/8000 [=====================>........] - ETA: 4s - loss: 0.0578
6144/8000 [======================>.......] - ETA: 4s - loss: 0.0579
6208/8000 [======================>.......] - ETA: 4s - loss: 0.0580
6272/8000 [======================>.......] - ETA: 3s - loss: 0.0579
6336/8000 [======================>.......] - ETA: 3s - loss: 0.0579
6400/8000 [=======================>......] - ETA: 3s - loss: 0.0580
6464/8000 [=======================>......] - ETA: 3s - loss: 0.0580
6528/8000 [=======================>......] - ETA: 3s - loss: 0.0580
6592/8000 [=======================>......] - ETA: 3s - loss: 0.0581
6656/8000 [=======================>......] - ETA: 3s - loss: 0.0581
6720/8000 [========================>.....] - ETA: 2s - loss: 0.0581
6784/8000 [========================>.....] - ETA: 2s - loss: 0.0582
6848/8000 [========================>.....] - ETA: 2s - loss: 0.0582
6912/8000 [========================>.....] - ETA: 2s - loss: 0.0582
6976/8000 [=========================>....] - ETA: 2s - loss: 0.0582
7040/8000 [=========================>....] - ETA: 2s - loss: 0.0583
7104/8000 [=========================>....] - ETA: 2s - loss: 0.0583
7168/8000 [=========================>....] - ETA: 1s - loss: 0.0584
7232/8000 [==========================>...] - ETA: 1s - loss: 0.0584
7296/8000 [==========================>...] - ETA: 1s - loss: 0.0585
7360/8000 [==========================>...] - ETA: 1s - loss: 0.0586
7424/8000 [==========================>...] - ETA: 1s - loss: 0.0587
7488/8000 [===========================>..] - ETA: 1s - loss: 0.0587
7552/8000 [===========================>..] - ETA: 1s - loss: 0.0587
7616/8000 [===========================>..] - ETA: 0s - loss: 0.0587
7680/8000 [===========================>..] - ETA: 0s - loss: 0.0587
7744/8000 [============================>.] - ETA: 0s - loss: 0.0587
7808/8000 [============================>.] - ETA: 0s - loss: 0.0588
7872/8000 [============================>.] - ETA: 0s - loss: 0.0588
7936/8000 [============================>.] - ETA: 0s - loss: 0.0588
8000/8000 [==============================] - 19s 2ms/step - loss: 0.0588 - val_loss: 0.7629
E:\keras-master\keras\engine\topology.py:2365: UserWarning: Layer lstm_2 was passed non-serializable keyword arguments: {'initial_state': [<tf.Tensor 'lstm_1/while/Exit_2:0' shape=(?, 256) dtype=float32>, <tf.Tensor 'lstm_1/while/Exit_3:0' shape=(?, 256) dtype=float32>]}. They will not be included in the serialized model (and thus will be missing at deserialization time).
  str(node.arguments) + '. They will not be included '
-
Input sentence: Go.
Decoded sentence: Va !

-
Input sentence: Run!
Decoded sentence: Cours !

-
Input sentence: Run!
Decoded sentence: Cours !

-
Input sentence: Fire!
Decoded sentence: Au feu !

-
Input sentence: Help!
Decoded sentence: À l'aide !

-
Input sentence: Jump.
Decoded sentence: Saute.

-
Input sentence: Stop!
Decoded sentence: Arrête-toi !

-
Input sentence: Stop!
Decoded sentence: Arrête-toi !

-
Input sentence: Stop!
Decoded sentence: Arrête-toi !

-
Input sentence: Wait!
Decoded sentence: Attends !

-
Input sentence: Wait!
Decoded sentence: Attends !

-
Input sentence: Go on.
Decoded sentence: Continuez.

-
Input sentence: Go on.
Decoded sentence: Continuez.

-
Input sentence: Go on.
Decoded sentence: Continuez.

-
Input sentence: I see.
Decoded sentence: Je vois une lumière.

-
Input sentence: I try.
Decoded sentence: J'essaye.

-
Input sentence: I won!
Decoded sentence: J'ai demandé à dore.

-
Input sentence: I won!
Decoded sentence: J'ai demandé à dore.

-
Input sentence: Oh no!
Decoded sentence: Oh non !

-
Input sentence: Attack!
Decoded sentence: Attaquez !

-
Input sentence: Attack!
Decoded sentence: Attaquez !

-
Input sentence: Cheers!
Decoded sentence: À votre santé !

-
Input sentence: Cheers!
Decoded sentence: À votre santé !

-
Input sentence: Cheers!
Decoded sentence: À votre santé !

-
Input sentence: Cheers!
Decoded sentence: À votre santé !

-
Input sentence: Get up.
Decoded sentence: Lève-toi.

-
Input sentence: Go now.
Decoded sentence: Va doucement !

-
Input sentence: Go now.
Decoded sentence: Va doucement !

-
Input sentence: Go now.
Decoded sentence: Va doucement !

-
Input sentence: Got it!
Decoded sentence: Compris !

-
Input sentence: Got it!
Decoded sentence: Compris !

-
Input sentence: Got it?
Decoded sentence: Compris ?

-
Input sentence: Got it?
Decoded sentence: Compris ?

-
Input sentence: Got it?
Decoded sentence: Compris ?

-
Input sentence: Hop in.
Decoded sentence: Montez.

-
Input sentence: Hop in.
Decoded sentence: Montez.

-
Input sentence: Hug me.
Decoded sentence: Serre-moi dans tes bras !

-
Input sentence: Hug me.
Decoded sentence: Serre-moi dans tes bras !

-
Input sentence: I fell.
Decoded sentence: Je suis tombée.

-
Input sentence: I fell.
Decoded sentence: Je suis tombée.

-
Input sentence: I know.
Decoded sentence: Je sais.

-
Input sentence: I left.
Decoded sentence: Je suis parti.

-
Input sentence: I left.
Decoded sentence: Je suis parti.

-
Input sentence: I lost.
Decoded sentence: J'ai perdu.

-
Input sentence: I'm 19.
Decoded sentence: J'ai les chocontes.

-
Input sentence: I'm OK.
Decoded sentence: Je vais bien.

-
Input sentence: I'm OK.
Decoded sentence: Je vais bien.

-
Input sentence: Listen.
Decoded sentence: Écoutez !

-
Input sentence: No way!
Decoded sentence: C'est exclu !

-
Input sentence: No way!
Decoded sentence: C'est exclu !

-
Input sentence: No way!
Decoded sentence: C'est exclu !

-
Input sentence: No way!
Decoded sentence: C'est exclu !

-
Input sentence: No way!
Decoded sentence: C'est exclu !

-
Input sentence: No way!
Decoded sentence: C'est exclu !

-
Input sentence: No way!
Decoded sentence: C'est exclu !

-
Input sentence: No way!
Decoded sentence: C'est exclu !

-
Input sentence: No way!
Decoded sentence: C'est exclu !

-
Input sentence: Really?
Decoded sentence: Vrai ?

-
Input sentence: Really?
Decoded sentence: Vrai ?

-
Input sentence: Really?
Decoded sentence: Vrai ?

-
Input sentence: Thanks.
Decoded sentence: Merci !

-
Input sentence: We try.
Decoded sentence: On essaye.

-
Input sentence: We won.
Decoded sentence: Nous avons réveillé.

-
Input sentence: We won.
Decoded sentence: Nous avons réveillé.

-
Input sentence: We won.
Decoded sentence: Nous avons réveillé.

-
Input sentence: We won.
Decoded sentence: Nous avons réveillé.

-
Input sentence: Ask Tom.
Decoded sentence: Demande-leur.

-
Input sentence: Awesome!
Decoded sentence: Faisalez-moi !

-
Input sentence: Be calm.
Decoded sentence: Sois calme !

-
Input sentence: Be calm.
Decoded sentence: Sois calme !

-
Input sentence: Be calm.
Decoded sentence: Sois calme !

-
Input sentence: Be cool.
Decoded sentence: Sois détendu !

-
Input sentence: Be fair.
Decoded sentence: Soyez équitables !

-
Input sentence: Be fair.
Decoded sentence: Soyez équitables !

-
Input sentence: Be fair.
Decoded sentence: Soyez équitables !

-
Input sentence: Be fair.
Decoded sentence: Soyez équitables !

-
Input sentence: Be fair.
Decoded sentence: Soyez équitables !

-
Input sentence: Be fair.
Decoded sentence: Soyez équitables !

-
Input sentence: Be kind.
Decoded sentence: Sois gentil.

-
Input sentence: Be nice.
Decoded sentence: Sois gentille !

-
Input sentence: Be nice.
Decoded sentence: Sois gentille !

-
Input sentence: Be nice.
Decoded sentence: Sois gentille !

-
Input sentence: Be nice.
Decoded sentence: Sois gentille !

-
Input sentence: Be nice.
Decoded sentence: Sois gentille !

-
Input sentence: Be nice.
Decoded sentence: Sois gentille !

-
Input sentence: Beat it.
Decoded sentence: Dégage !

-
Input sentence: Call me.
Decoded sentence: Appellez-moi !

-
Input sentence: Call me.
Decoded sentence: Appellez-moi !

-
Input sentence: Call us.
Decoded sentence: Appelle-nous !

-
Input sentence: Call us.
Decoded sentence: Appelle-nous !

-
Input sentence: Come in.
Decoded sentence: Entrez !

-
Input sentence: Come in.
Decoded sentence: Entrez !

-
Input sentence: Come in.
Decoded sentence: Entrez !

-
Input sentence: Come in.
Decoded sentence: Entrez !

-
Input sentence: Come on!
Decoded sentence: Allez !

-
Input sentence: Come on.
Decoded sentence: Viens !

-
Input sentence: Come on.
Decoded sentence: Viens !

-
Input sentence: Come on.
Decoded sentence: Viens !

-
Input sentence: Drop it!
Decoded sentence: Laissez-le tomber !

-
Input sentence: Drop it!
Decoded sentence: Laissez-le tomber !


Process finished with exit code 0

Keras详细介绍

英文:https://keras.io/

中文:http://keras-cn.readthedocs.io/en/latest/

实例下载

https://github.com/keras-team/keras

https://github.com/keras-team/keras/tree/master/examples

完整项目下载

方便没积分童鞋,请加企鹅452205574,共享文件夹。

包括:代码、数据集合(图片)、已生成model、安装库文件等。


  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
下面代码在tensorflow中出现了init() missing 1 required positional argument: 'cell'报错: class Model(): def init(self): self.img_seq_shape=(10,128,128,3) self.img_shape=(128,128,3) self.train_img=dataset # self.test_img=dataset_T patch = int(128 / 2 ** 4) self.disc_patch = (patch, patch, 1) self.optimizer=tf.keras.optimizers.Adam(learning_rate=0.001) self.build_generator=self.build_generator() self.build_discriminator=self.build_discriminator() self.build_discriminator.compile(loss='binary_crossentropy', optimizer=self.optimizer, metrics=['accuracy']) self.build_generator.compile(loss='binary_crossentropy', optimizer=self.optimizer) img_seq_A = Input(shape=(10,128,128,3)) #输入图片 img_B = Input(shape=self.img_shape) #目标图片 fake_B = self.build_generator(img_seq_A) #生成的伪目标图片 self.build_discriminator.trainable = False valid = self.build_discriminator([img_seq_A, fake_B]) self.combined = tf.keras.models.Model([img_seq_A, img_B], [valid, fake_B]) self.combined.compile(loss=['binary_crossentropy', 'mse'], loss_weights=[1, 100], optimizer=self.optimizer,metrics=['accuracy']) def build_generator(self): def res_net(inputs, filters): x = inputs net = conv2d(x, filters // 2, (1, 1), 1) net = conv2d(net, filters, (3, 3), 1) net = net + x # net=tf.keras.layers.LeakyReLU(0.2)(net) return net def conv2d(inputs, filters, kernel_size, strides): x = tf.keras.layers.Conv2D(filters, kernel_size, strides, 'same')(inputs) x = tf.keras.layers.BatchNormalization()(x) x = tf.keras.layers.LeakyReLU(alpha=0.2)(x) return x d0 = tf.keras.layers.Input(shape=(10, 128, 128, 3)) out= ConvRNN2D(filters=32, kernel_size=3,padding='same')(d0) out=tf.keras.layers.Conv2D(3,1,1,'same')(out) return keras.Model(inputs=d0, outputs=out) def build_discriminator(self): def d_layer(layer_input, filters, f_size=4, bn=True): d = tf.keras.layers.Conv2D(filters, kernel_size=f_size, strides=2, padding='same')(layer_input) if bn: d = tf.keras.layers.BatchNormalization(momentum=0.8)(d) d = tf.keras.layers.LeakyReLU(alpha=0.2)(d) return d img_A = tf.keras.layers.Input(shape=(10, 128, 128, 3)) img_B = tf.keras.layers.Input(shape=(128, 128, 3)) df = 32 lstm_out = ConvRNN2D(filters=df, kernel_size=4, padding="same")(img_A) lstm_out = tf.keras.layers.LeakyReLU(alpha=0.2)(lstm_out) combined_imgs = tf.keras.layers.Concatenate(axis=-1)([lstm_out, img_B]) d1 = d_layer(combined_imgs, df)#64 d2 = d_layer(d1, df * 2)#32 d3 = d_layer(d2, df * 4)#16 d4 = d_layer(d3, df * 8)#8 validity = tf.keras.layers.Conv2D(1, kernel_size=4, strides=1, padding='same')(d4) return tf.keras.Model([img_A, img_B], validity)
05-17

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值