文章を学習するdeep autoencoderを書こうと試みています。
しかし、
C:\Users\yudai\Desktop\keras_AE.py:62: UserWarning: Update your `Model` call to the Keras 2 API: `Model(inputs=Tensor("in..., outputs=Tensor("de...)` autoencoder = Model(input=input_word, output=decoded) Traceback (most recent call last): File "C:\Users\yudai\Desktop\keras_AE.py", line 70, in <module> shuffle=False) File "C:\Users\yudai\Anaconda3\envs\pyMLgpu\lib\site-packages\keras\engine\training.py", line 1039, in fit validation_steps=validation_steps) File "C:\Users\yudai\Anaconda3\envs\pyMLgpu\lib\site-packages\keras\engine\training_arrays.py", line 139, in fit_loop if issparse(ins[i]) and not K.is_sparse(feed[i]): IndexError: list index out of range
と出力されます。
もし原因がわかる方がいらっしゃるならば、
何卒、ご教授宜しくお願い致します。
スタックオーバーフローでも質問しています。
マルチポストです。すみません。
追記:
https://github.com/keras-team/keras/issues/7602
より
autoencoder = Model(input=input_word, output=decoded)
を
autoencoder = Model(inputs=input_word, output=decoded)
に直しました。
しかし、同じエラーが出ます。
違うWindows 10のPCでは、
python 3.6.5
tensorflow 1.8.0
keras 2.1.5
C:\Users\hoge\Desktop\keras_AE.py:62: UserWarning: Update your Model call to the Keras 2 API: Model(inputs=Tensor("in..., outputs=Tensor("de...) autoencoder = Model(input=input_word, output=decoded) Traceback (most recent call last): File "C:\Users\hoge\Desktop\keras_AE.py", line 70, in shuffle=False) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\keras\engine\training.py", line 1630, in fit batch_size=batch_size) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\keras\engine\training.py", line 1487, in _standardize_user_data in zip(y, sample_weights, class_weights, self._feed_sample_weight_modes)] File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\keras\engine\training.py", line 1486, in for (ref, sw, cw, mode) File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\keras\engine\training.py", line 540, in _standardize_weights return np.ones((y.shape[0],), dtype=K.floatx()) AttributeError: 'NoneType' object has no attribute 'shape'
が同じコードで違うエラーがでます。
python
1# -*- coding: utf-8 -*- 2from keras.layers import Input, Dense 3from keras.layers.core import Activation 4from keras.models import Model 5from keras.utils.data_utils import get_file 6import numpy as np 7import codecs 8 9#データの読み込み 10with codecs.open(r'C:\Users\yudai\Desktop\poem.txt', 'r', 'utf-8') as f: 11 for text in f: 12 text = text.strip() 13#コーパスの長さ 14print('corpus length:', len(text)) 15#文字数を数えるため、textをソート 16chars = sorted(list(set(text))) 17#全文字数の表示 18print('total chars:', len(chars)) 19#文字をID変換 20char_indices = dict((c, i) for i, c in enumerate(chars)) 21#IDから文字へ変換 22indices_char = dict((i, c) for i, c in enumerate(chars)) 23#テキストを17文字ずつ読み込む 24maxlen = 17 25#サンプルバッチ数 26step = 3 27sentences = [] 28next_chars = [] 29for i in range(0, len(text) - maxlen, step): 30 sentences.append(text[i: i + maxlen]) 31 next_chars.append(text[i + maxlen]) 32#学習する文字数を表示 33print('Sequences:', len) 34 35#ベクトル化する 36print('Vectorization...') 37x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool) 38y = np.zeros((len(sentences), len(chars)), dtype=np.bool) 39for i, sentence in enumerate(sentences): 40 for t, char in enumerate(sentence): 41 x[i, t, char_indices[char]] = 1 42 y[i, char_indices[next_chars[i]]] = 1 43 44#モデルを構築する工程に入る 45print('Build model...') 46#encoderの次元 47encoding_dim = 128 48#入力用の変数 49input_word = Input(shape=(maxlen, len(chars))) 50#入力された語がencodeされたものを格納する 51encoded = Dense(128, activation='relu')(input_word) 52encoded = Dense(64, activation='relu')(encoded) 53encoded = Dense(32, activation='relu')(encoded) 54#潜在変数(実質的な主成分分析) 55latent = Dense(8, activation='relu')(encoded) 56#encodeされたデータを再構成 57decoded = Dense(32, activation='relu')(latent) 58decoded = Dense(64, activation='relu')(decoded) 59decoded = Dense(128, activation='relu')(encoded) 60 61output = Dense(100, activation='relu') 62 63autoencoder = Model(input=input_word, output=decoded) 64#Adamで最適化、loss関数をcategorical_crossentropy 65autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy') 66 67#autoencoderの実行 68autoencoder.fit(x, 69 epochs=1000, 70 batch_size=256, 71 shuffle=False) 72#学習の進み具合を観察 73def on_epoch_end(epochs): 74 print() 75 print('Epoch: %d' % epochs) 76 77#モデルの構造を保存 78model_json = autoencoder.to_json() 79with open('keras_AE.json', 'w') as json_file: 80 json_file.write(model_json) 81#学習済みモデルの重みを保存 82autoencoder.save_weights('AE.h5') 83 84decoded_word = autoencoder.predict(word_test) 85 86X_embedded = model.predict(X_train) 87autoencoder.fit(X_embedded,X_embedded,epochs=10, 88 batch_size=256, validation_split=.1)
C:\Users\yudai\Desktop\poem.txtは、webから俳句を29000件集め、MeCabで形態素解析しております。
例:
朝霧 の 中 に 九段 の ともし 哉
あたたか な 雨 が 降る なり 枯葎
菜の花 や は つと 明るき 町 は づれ
秋風 や 伊予 へ 流る る 汐 の 音
長閑 さ や 障子 の 穴 に 海 見え て
若鮎 の 二 手 に なりて 上り けり
行く 秋 を す つく と 鹿 の 立ち に けり
我 声 の 風 に なり けり 茸狩
毎年 よ 彼岸の入り に 寒い の は
#環境
Windows 10
python 3.7.0
tensorflow-gpu 1.9.0
keras 2.2.4
回答1件
あなたの回答
tips
プレビュー