やりたいこと
にあるようなモデルを作成して分類問題を解く。
What is the difference between stacked LSTM's and multidimensional LSTM's? - Quora
を参考にしつつ(してない...?)モデルを作成。 Wen, Tsung-Hsien, et al. "Semantically conditioned lstm-based natural language generation for spoken dialogue systems." arXiv preprint arXiv:1508.01745 (2015) に図が載っているので参照。
コード
# インポート from keras.datasets import imdb from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from keras.layers.embeddings import Embedding from keras.preprocessing import sequence import tensorflow as tf import pandas as pd import numpy as np import matplotlib.pyplot as plt;plt.style.use('ggplot') import seaborn as sns; sns.set() # データをロード X_array, y_array = loaddata() # 適宜ロード X_array.shape = (サンプル数、各特徴の時系列の長さ、データの特徴数) X, y = X_array.copy(), np.array(pd.get_dummies(y_array)) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, random_state=42) # ハイパーーパラメータ DATA_DIM = X.shape[2] TIMESTAMP_LENGHT = X.shape[1] nb_classes = y.shape[1] BATCH_SIZE = 50 EPOCH = 50 LSTMDIM = 32 # モデルの定義 model = Sequential() model.add(LSTM(LSTMDIM, return_sequences=True, input_shape=(timesteps, data_dim))) model.add(LSTM(LSTMDIM, return_sequences=True)) model.add(LSTM(LSTMDIM)) model.add(Dense(nb_classes, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # モデルの訓練 model.fit(X_train, y_train, batch_size=BATCH_SIZE, nb_epoch=EPOCH, validation_data=(X_test, y_test))