forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07shared_lstm.py
More file actions
25 lines (20 loc) · 853 Bytes
/
07shared_lstm.py
File metadata and controls
25 lines (20 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# -*- coding: utf-8 -*-
# Author: XuMing <[email protected]>
# Brief:
import keras
import numpy as np
tweet_a = keras.layers.Input(shape=(280, 256))
tweet_b = keras.layers.Input(shape=(280, 256))
shared_lstm = keras.layers.LSTM(64)
encoded_a = shared_lstm(tweet_a)
encoded_b = shared_lstm(tweet_b)
# concatenate the two vectors
merged_vector = keras.layers.concatenate([encoded_a, encoded_b], axis=-1)
preds = keras.layers.Dense(1, activation='sigmoid')(merged_vector)
model = keras.models.Model(inputs=[tweet_a, tweet_b], outputs=preds)
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
data_a = np.random.random((1000, 280, 256))
data_b = np.random.random((1000, 280, 256))
labels = np.random.randint(2, size=(1000, 1))
model.fit([data_a, data_b], labels, epochs=10, batch_size=64)
# loss: 0.5961 - acc: 0.7230