Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
likorn
/
ml-lstm-project
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
49615313
authored
Jan 01, 2019
by
Paktalin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit
parents
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
41 additions
and
0 deletions
try.py
try.py
0 → 100644
View file @
49615313
from
keras.models
import
Sequential
from
keras.layers
import
Dense
,
Embedding
,
LSTM
from
keras.datasets
import
imdb
from
keras.preprocessing
import
sequence
import
numpy
as
np
max_features
=
10000
maxlen
=
400
batch_size
=
50
# load data
(
x_train
,
y_train
),
(
x_test
,
y_test
)
=
imdb
.
load_data
(
num_words
=
max_features
)
# pad sequences
x_train
=
sequence
.
pad_sequences
(
x_train
,
maxlen
=
maxlen
)
x_test
=
sequence
.
pad_sequences
(
x_test
,
maxlen
=
maxlen
)
# split test and validation sets
val_len
=
int
(
len
(
y_test
)
*
0.15
)
x_validation
,
y_validation
=
x_test
[:
val_len
,:],
y_test
[:
val_len
]
x_test
,
y_test
=
x_test
[
val_len
:,
:],
y_test
[
val_len
:]
# create LSTM model with 512 units and dropout
model
=
Sequential
()
model
.
add
(
Embedding
(
max_features
,
50
,
input_length
=
maxlen
,
batch_input_shape
=
(
batch_size
,
maxlen
,)))
model
.
add
(
LSTM
(
512
,
dropout
=
0.2
,
recurrent_dropout
=
0.2
,
stateful
=
True
))
model
.
add
(
Dense
(
1
,
activation
=
'sigmoid'
))
# compile model
model
.
compile
(
optimizer
=
'rmsprop'
,
loss
=
'binary_crossentropy'
,
metrics
=
[
'acc'
])
print
(
model
.
summary
())
# train model
model
.
fit
(
x_train
,
y_train
,
batch_size
=
batch_size
,
epochs
=
15
,
validation_data
=
(
x_validation
,
y_validation
))
#save model
model
.
save
(
'my_model.h5'
)
# print model's score and accuracy
print
(
'Score:
%
f
\n
Accuracy:
%
f'
%
model
.
evaluate
(
x_test
,
y_test
,
batch_size
=
batch_size
))
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment