質問編集履歴

1 タイトルの変更

yep

yep score 39

2019/02/03 18:55  投稿

#やりたいこと
kerasのexample、[https://github.com/keras-team/keras/blob/master/examples/lstm_seq2seq.py](https://github.com/keras-team/keras/blob/master/examples/lstm_seq2seq.py)(**オリジナル**)を基に
一つの情報から複数の情報をseq2seqで推定したい(**アレンジ**)と考えています。
#起きている問題
Python 3.6.4 :: Anaconda, Inc.
keras 2.2.4
tensorflow 1.12.0 gpu_py36he74679b_0
numpy 1.15.4
```ここに言語を入力
Using TensorFlow backend.
Max sequence length for input_image: 2099200
Max sequence length for integer: 2099200
Max sequence length for exponent: 2099200
Traceback (most recent call last):
File "./Desktop/seq2seq_hdf.py", line 130, in <module>
validation_split=0.2)
File "/home/yudai/.local/lib/python3.6/site-packages/keras/engine/training.py", line 952, in fit
batch_size=batch_size)
File "/home/yudai/.local/lib/python3.6/site-packages/keras/engine/training.py", line 677, in _standardize_user_data
self._set_inputs(x)
File "/home/yudai/.local/lib/python3.6/site-packages/keras/engine/training.py", line 636, in _set_inputs
outputs = self.call(unpack_singleton(self.inputs))
File "/home/yudai/.local/lib/python3.6/site-packages/keras/engine/network.py", line 561, in call
if cache_key in self._output_tensor_cache:
AttributeError: 'Model' object has no attribute '_output_tensor_cache'
```
#試したこと
'_output_tensor_cache'のエラーについて調べてみたところ、
[https://stackoverflow.com/questions/49881096/how-to-fix-error-object-has-no-attribute-output-tensor-cache](https://stackoverflow.com/questions/49881096/how-to-fix-error-object-has-no-attribute-output-tensor-cache)
[https://github.com/keras-team/keras/issues/9310](https://github.com/keras-team/keras/issues/9310)
上記の同じエラーのサイトを見つけました。
AttributeErrorは、属性名、メソッド名が違うことによって起きるエラーで、
今回の場合は、keras、tensorflowの**ダウングレード**を行うと解決できるのではと考えていましたが、別のエラーを引き起こしました。
python 3.5
keras:2.1.2
tensorflow:1.1.0_np112py35_0
numpy:1.12.1
```ここに言語を入力
Using TensorFlow backend.
Max sequence length for input_image: 2099200
Max sequence length for integer: 2099200
Max sequence length for exponent: 2099200
Traceback (most recent call last):
File "/home/yudai/Desktop/seq2seq.py", line 122, in <module>
decoder_integer_output, decoder_exponent_output, decoder_sign_output)
File "/home/yudai/.pyenv/versions/anaconda3-5.3.1/envs/gpu/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
TypeError: __init__() takes from 2 to 3 positional arguments but 4 were given
```
kerasやtensorflowの**Model**についても調べましたが、どう直せば良いのか分かりませんでした。
kerasのModel(functional API)
[https://keras.io/ja/models/model/](https://keras.io/ja/models/model/)
tensorflowのModel
[https://www.tensorflow.org/api_docs/python/tf/keras/models/Model](https://www.tensorflow.org/api_docs/python/tf/keras/models/Model)
#アレンジ
```ここに言語を入力
from keras.layers import Input, LSTM, Dense, Concatenate
from keras.models import Model
import numpy as np
batch_size = 64
epochs = 100
latent_dim = 256
input_image = [None] * 2
for i in range(2):
with open('./Desktop//speedy/input/{}.txt'.format(i + 1), mode='r', encoding='utf-8')as f:
input_image[i] = f.read()
target_integer = [None] * 2
for i in range(2):
with open('./Desktop/speedy/target_integer/{}.txt'.format(i + 1), mode='r', encoding='utf-8')as f:
target_integer[i] = f.read()
target_exponent = [None] * 2
for i in range(2):
with open('./Desktop/speedy/target_exponent/{}.txt'.format(i + 1), mode='r', encoding='utf-8')as f:
target_exponent[i] = f.read()
input_characters = [None] * 2
for i in range(2):
with open('./Desktop/speedy/input/{}.txt'.format(i + 1), mode='r', encoding='utf-8')as f:
input_characters[i] = f.read()
target_integer_characters = [None] * 2
for i in range(2):
with open('./Desktop/speedy/target_integer/{}.txt'.format(i + 1), mode='r', encoding='utf-8')as f:
target_integer_characters[i] = f.read()
target_exponent_characters = [None] * 2
for i in range(2):
with open('./Desktop/speedy/target_exponent/{}.txt'.format(i + 1), mode='r', encoding='utf-8')as f:
target_exponent_characters[i] = f.read()
input_images = map(str, input_image)
target_integers = map(str, target_integer)
target_exponents = map(str, target_exponent)
input_characters = set(input_characters)
target_integer_characters = set(target_integer_characters)
target_exponent_characters = set(target_exponent_characters)
for char in input_images:
if char not in input_characters:
input_characters.add(char)
for char in target_integers:
if char not in target_integer_characters:
target_integer_characters.add(char)
for char in target_exponents:
if char not in target_exponent_characters:
target_exponent_characters.add(char)
input_char = sorted(list(input_characters))
target_integer_char = sorted(list(target_integer_characters))
target_exponent_char = sorted(list(target_exponent_characters))
num_encoder_tokens = len(input_char)
num_decoder_integer_tokens = len(target_integer_char)
num_decoder_exponent_tokens = len(target_exponent_char)
max_encoder_seq_length = max([len(number) for number in input_image])
max_decoder_integer_seq_length = max([len(integer) for integer in target_integer])
max_decoder_exponent_seq_length = max([len(exponent) for exponent in target_exponent])
input_token_index = dict(
[(char, i) for i, char in enumerate(input_char)])
target_integer_token_index = dict(
[(char, i) for i, char in enumerate(target_integer_char)])
target_exponent_token_index = dict(
[(char, i) for i, char in enumerate(target_exponent_char)])
encoder_input_data = np.zeros(
(len(input_image), max_encoder_seq_length, num_encoder_tokens),
dtype='float32')
decoder_integer_input_data = np.zeros(
(len(input_image), max_decoder_integer_seq_length, num_decoder_integer_tokens),
dtype='float32')
decoder_exponent_input_data = np.zeros(
(len(input_image), max_decoder_exponent_seq_length, num_decoder_exponent_tokens),
dtype='float32')
decoder_integer_target_data = np.zeros(
(len(input_image), max_decoder_integer_seq_length, num_decoder_integer_tokens),
dtype='float32')
decoder_exponent_target_data = np.zeros(
(len(input_image), max_decoder_exponent_seq_length, num_decoder_exponent_tokens),
dtype='float32')
for i, (input_images, target_integers, target_exponents) in enumerate(zip(input_image, target_integer, target_exponent)):
for t, char in enumerate(input_images):
encoder_input_data[i, t, input_token_index[char]] = 1.
for t, char in enumerate(taregt_integers):
decoder_input_data[i, t, target_integer_token_index[char]] = 1.
for t, char in enumerate(taregt_exponents):
decoder_input_data[i, t, target_exponent_token_index[char]] = 1.
if t > 0:
decoder_integer_target_data[i, t -1, target_integer_token_index[char]] = 1.
decoder_exponent_target_data[i, t -1, target_integer_token_index[char]] = 1.
encoder_inputs = Input(shape=(None, num_encoder_tokens))
integer_input = Input(shape=(None, num_encoder_integer_tokens))
exponent_input = Input(shape=(None, num_encoder_exponent_tokens))
main_input = keras.layers.concatenate([integer_input, exponent_input)
encoder_inputs = keras.layers.concatenate([main_input, encoder_inputs])
encoder = LSTM(latent_dim, return_state=True)
encoder_outputs, state_h, state_c = encoder(encoder_inputs)
encoder_states = [state_h, state_c]
decoder_integer_input = Input(shape=(None, num_decoder_integer_tokens))
decoder_exponent_input = Input(shape=(None, num_decoder_exponent_tokens))
decoder_inputs = keras.layers.concatenate([decoder_integer_input, decoder_exponent_input])
integer_decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True)
exponent_decoder_lstm = LSTM(latent_dim, return_sequences=True, return_state=True)
integer_decoder_outputs, integer_, integer_ = decoder_lstm(integer_decoder_inputs, initial_state=encoder_states)
exponent_decoder_outputs, exponent_, exponent_ = decoder_lstm(exponent_decoder_inputs, initial_state=encoder_states)
integer_decoder_dense = Dense(num_decoder_integer_tokens, activation='softmax')
exponent_decoder_dense = Dense(num_decoder_exponent_tokens, activation='softmax')
decoder_integer_output = decoder_dense(decoder_dense[decoder_integer_output])
decoder_exponent_output = decoder_dense(decoder_dense[decoder_exponent_output])
model = Model(encoder_inputs=[encoder_inputs, decoder_inputs],
decoder_outputs=[integer_outputs, exponent_outputs])
model.compile(optimizer='rmsprop', loss='categorical_crossentry')
model.fit([encoder_input_data, decoder_input_data],
decoder_integer_target_data, decoder_exponent_target_data,
batch_size=batch_size,
epochs=epochs,
validation_split=0.2)
model.save_weights('./Desktop/seq2eq_num.h5')
model.summary()
encoder_model = Model(encoder_inputs, encoder_states)
```
#オリジナル
```ここに言語を入力
字数制限によりオリジナルのコードを載せれませんでした。
オリジナルにおけるアレンジの該当コード部分は、51から153行目までとなっています。
```
**オリジナル**
[https://github.com/keras-team/keras/blob/master/examples/lstm_seq2seq.py](https://github.com/keras-team/keras/blob/master/examples/lstm_seq2seq.py)
  • Python

    11670 questions

    Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

  • Keras

    471 questions

思考するエンジニアのためのQ&Aサイト「teratail」について詳しく知る