文章を学習する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
# -*- coding: utf-8 -*- from keras.layers import Input, Dense from keras.layers.core import Activation from keras.models import Model from keras.utils.data_utils import get_file import numpy as np import codecs #データの読み込み with codecs.open(r'C:\Users\yudai\Desktop\poem.txt', 'r', 'utf-8') as f: for text in f: text = text.strip() #コーパスの長さ print('corpus length:', len(text)) #文字数を数えるため、textをソート chars = sorted(list(set(text))) #全文字数の表示 print('total chars:', len(chars)) #文字をID変換 char_indices = dict((c, i) for i, c in enumerate(chars)) #IDから文字へ変換 indices_char = dict((i, c) for i, c in enumerate(chars)) #テキストを17文字ずつ読み込む maxlen = 17 #サンプルバッチ数 step = 3 sentences = [] next_chars = [] for i in range(0, len(text) - maxlen, step): sentences.append(text[i: i + maxlen]) next_chars.append(text[i + maxlen]) #学習する文字数を表示 print('Sequences:', len) #ベクトル化する print('Vectorization...') x = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool) y = np.zeros((len(sentences), len(chars)), dtype=np.bool) for i, sentence in enumerate(sentences): for t, char in enumerate(sentence): x[i, t, char_indices[char]] = 1 y[i, char_indices[next_chars[i]]] = 1 #モデルを構築する工程に入る print('Build model...') #encoderの次元 encoding_dim = 128 #入力用の変数 input_word = Input(shape=(maxlen, len(chars))) #入力された語がencodeされたものを格納する encoded = Dense(128, activation='relu')(input_word) encoded = Dense(64, activation='relu')(encoded) encoded = Dense(32, activation='relu')(encoded) #潜在変数(実質的な主成分分析) latent = Dense(8, activation='relu')(encoded) #encodeされたデータを再構成 decoded = Dense(32, activation='relu')(latent) decoded = Dense(64, activation='relu')(decoded) decoded = Dense(128, activation='relu')(encoded) output = Dense(100, activation='relu') autoencoder = Model(input=input_word, output=decoded) #Adamで最適化、loss関数をcategorical_crossentropy autoencoder.compile(optimizer='Adam', loss='categorical_crossentropy') #autoencoderの実行 autoencoder.fit(x, epochs=1000, batch_size=256, shuffle=False) #学習の進み具合を観察 def on_epoch_end(epochs): print() print('Epoch: %d' % epochs) #モデルの構造を保存 model_json = autoencoder.to_json() with open('keras_AE.json', 'w') as json_file: json_file.write(model_json) #学習済みモデルの重みを保存 autoencoder.save_weights('AE.h5') decoded_word = autoencoder.predict(word_test) X_embedded = model.predict(X_train) autoencoder.fit(X_embedded,X_embedded,epochs=10, 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
まだ回答がついていません
会員登録して回答してみよう