Last active
September 2, 2018 08:10
-
-
Save mercy0387/7518e2c448667fa81837995309b3727e to your computer and use it in GitHub Desktop.
kerasで頭に描いたネットワーク構造を実現するためのTips ~ functional API 編 ~ ref: https://qiita.com/Mco7777/items/1339d01bc6ef028e7b44
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
y_i = \sum_{k=1}^{5} (w_{ki}*x_k) + b_i \quad (1 \leq i \leq 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Model | |
from keras.layers import Input, Dense, Activation | |
model_input = Input(shape=(5,)) | |
mid = Dense(10)(model_input) | |
output = Activation('softmax')(mid) | |
model = Model(inputs=model_input, outputs=output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_dense = Dense(10) | |
my_activation = Activation('softmax') | |
model_input = Input(shape=(5,)) | |
mid = my_dense(model_input) | |
output = my_activation(mid) | |
model = Model(inputs=model_input, outputs=output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my_dense = Dense(5) | |
model_input = Input(shape=(5,)) | |
mid = my_dense(model_input) | |
mid2 = my_dense(mid) | |
output = Activation('softmax')(mid2) | |
model = Model(inputs=model_input, outputs=output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from keras import backend as K | |
# 適当な学習 | |
model.compile(loss='mean_squared_error', optimizer='adam') | |
X = np.random.rand(10,5) | |
y = np.random.rand(10,5) | |
model.fit(X, y, epochs=5) | |
# 出力 | |
print([K.eval(w) for w in model.weights]) | |
# [array([[ 0.16185357, -0.15696804, -0.36604205, -0.02380839, -0.35026637], | |
# [-0.13462609, 0.09170605, 0.13241905, -0.628663 , -0.67678481], | |
# [ 0.15944755, -0.67250979, 0.35241356, 0.24243712, 0.10116834], | |
# [ 0.61587954, -0.04093929, 0.36012208, -0.30871043, -0.31387496], | |
# [ 0.66632921, -0.31295586, 0.59317648, 0.44108745, 0.09751857]], dtype=float32), | |
# array([ 0.00472282, 0.00472244, 0.00472797, 0.00468933, -0.00472205], dtype=float32)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from keras.models import Model | |
from keras.layers import Input, Dense, Activation, Multiply | |
model_input1 = Input(shape=(5,)) | |
model_input2 = Input(shape=(5,)) | |
mid = Dense(10)(model_input1) | |
mid2 = Dense(10)(model_input2) | |
multiplied = Multiply()([mid, mid2]) | |
additional_dense = Dense(5)(multiplied) | |
additional_dense2 = Dense(5)(additional_dense) | |
output1 = Activation('softmax')(additional_dense) | |
output2 = Activation('softmax')(additional_dense2) | |
model = Model(inputs=[model_input1, model_input2], outputs=[output1, output2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment