6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Kerasを使ったAutoEncodderのメモ

Posted at

#はじめに
Kerasのautoencoderについて,以下のブログを参照した.
http://blog.keras.io/building-autoencoders-in-keras.html
ブログでは,MNISTを入力データとしている.

MNISTのような画像で特徴抽出をやる例は多いが,本記事では画像以外の信号を扱うことにする.

#前処理
前処理として,AutoEncoderの入力データをここでは-8~+8までの値をもつ加速度センサの計測データとする.
そして,データの変数をwindoWとする.そして,隠れ層のノード数を8個とする.

def SaveDicDataFromFileNPZ(PATH,name,data):
    if not ( os.path.exists(PATH) ): os.makedirs(PATH)

    np.savez(PATH+name, data=data)

# 各パラメータを保存するためにPATHを設定する
SensorName='sensor1'

SaveFileNameEncord=SensorName+'_AccX_encoded'
SaveFileNameDecord=SensorName+'_AccX_decoded'
SaveFileNameNet=SensorName+'_AccX_net'
SaveFileNameTrain=SensorName+'_AccX_train'
SaveFileNameTest=SensorName+'_AccX_test'
SaveFileNameGlaph=GlaphDataPath+SensorName+'_AccX_loss_val_loss.png'

window_test=windoW
window_train=windoW

encoding_dim = 8  
shapeNum=windoW.shape[0]*windoW.shape[1]

# this is our input placeholder
input_img = Input(shape=(shapeNum,))
# "encoded" is the encoded representation of the input
encoded = Dense(encoding_dim, activation='tanh')(input_img)
# "decoded" is the lossy reconstruction of the input
decoded = Dense(shapeNum, activation='linear')(encoded)

# this model maps an input to its reconstruction
autoencoder = Model(input=input_img, output=decoded)

# this model maps an input to its encoded representation
encoder = Model(input=input_img, output=encoded)

# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(encoding_dim,))
# retrieve the last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# create the decoder model
decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))

autoencoder.compile(optimizer='adadelta', loss='mse')
plot(autoencoder,  to_file=StudyDataModelPicPath+SaveFileNameNet+'.png')

hist = autoencoder.fit(window_train, window_train,
                nb_epoch=50,
                batch_size=shapeNum/4,
                shuffle=True,
                validation_data=(window_test, window_test))

# エンコード,デコードしたオブジェクトを保存する
encoded_imgs = encoder.predict(window_test)
decoded_imgs = decoder.predict(encoded_imgs)

processing.SaveDicDataFromFileNPZ(StudyDataPath,SaveFileNameEncord,encoded_imgs)
processing.SaveDicDataFromFileNPZ(StudyDataPath,SaveFileNameDecord,decoded_imgs)

# 各層の重みやバイアスなどのパラメータを保存する
json_string = encoder.to_json()
open(StudyDataPath+SaveFileNameEncord+'.json', 'w').write(json_string)
encoder.save_weights(StudyDataPath+SaveFileNameEncord+'_weights.h5')

json_string = decoder.to_json()
open(StudyDataPath+SaveFileNameDecord+'.json', 'w').write(json_string)
decoder.save_weights(StudyDataPath+SaveFileNameDecord+'_weights.h5')

json_string = autoencoder.to_json()
open(StudyDataPath+SaveFileNameNet+'.json', 'w').write(json_string)
autoencoder.save_weights(StudyDataPath+SaveFileNameNet+'_weights.h5')

# 損失をグラフに表示する
loss = hist.history['loss']
val_loss = hist.history['val_loss']

nb_epoch = len(loss)
plt.plot(range(nb_epoch), loss, marker='.', label='loss')
plt.plot(range(nb_epoch), val_loss, marker='.', label='val_loss')
plt.legend(loc='best', fontsize=10)
plt.grid()
plt.xlabel('epoch')
plt.ylabel('loss')
plt.savefig(SaveFileNameGlaph)
plt.show()

出力されるものは以下の図のようなグラフになる.

image

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?