Изменения

Перейти к: навигация, поиск

Обзор библиотек для машинного обучения на Python

723 байта добавлено, 11:29, 23 января 2019
Сверточная нейронная сеть
Реализация сверточной нейронной сети для классификации текста:
'''from ''' __future__ '''import ''' print_function '''from ''' keras.preprocessing '''import ''' sequence '''from ''' keras.models '''import ''' Sequential '''from ''' keras.layers '''import ''' Dense, Dropout, Activation '''from ''' keras.layers '''import ''' Embedding '''from ''' keras.layers '''import ''' Conv1D, GlobalMaxPooling1D '''from ''' keras.datasets '''import ''' imdb
<font color="green"># set parameters:</font> max_features = <font color="blue">5000</font> maxlen = <font color="blue">400</font> batch_size = <font color="blue">32</font> embedding_dims = <font color="blue">50</font> filters = <font color="blue">250</font> kernel_size = <font color="blue">3</font> hidden_dims = <font color="blue">250</font> epochs = <font color="blue">2</font>
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
print(len(x_train), <font color="red">'train sequences'</font>) print(len(x_test), <font color="red">'test sequences'</font>)
print(<font color="red">'Pad sequences (samples x time)'</font>)
x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
print(<font color="red">'x_train shape:'</font>, x_train.shape) print(<font color="red">'x_test shape:'</font>, x_test.shape)
model = Sequential()
embedding_dims,
input_length=maxlen))
model.add(Dropout(<font color="blue">0.2</font>))
model.add(Conv1D(filters,
kernel_size,
padding=<font color="red">'valid'</font>, activation=<font color="red">'relu'</font>, strides=<font color="blue">1</font>))
model.add(GlobalMaxPooling1D())
model.add(Dense(hidden_dims))
model.add(Dropout(<font color="blue">0.2</font>)) model.add(Activation(<font color="red">'relu'</font>)) model.add(Dense(<font color="blue">1</font>)) model.add(Activation(<font color="red">'sigmoid'</font>))
model.compile(loss=<font color="red">'binary_crossentropy'</font>, optimizer=<font color="red">'adam'</font>, metrics=[<font color="red">'accuracy'</font>])
model.fit(x_train, y_train,
batch_size=batch_size,
333
правки

Навигация