码迷,mamicode.com
首页 > 其他好文 > 详细

二分类问题

时间:2020-05-31 10:48:04      阅读:75      评论:0      收藏:0      [点我收藏+]

标签:oid   rms   Dimension   sha   model   load   精度   compile   sequence   

二分类问题

首先进行数据处理:

将读入的数据转成向量,将整数序列编码为二维矩阵

def v(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1.
    return results
x_train = v(train_data)
x_test = v(test_data)

标签同时也要向量化:

y_train = np.asarray(train_labels).astype(‘float32‘)
y_test = np.asarray(test_labels).astype(‘float32‘)

初始化、编译、添加层

model = models.Sequential()
model.add(layers.Dense(16,activation=‘relu‘,input_shape=(10000,)))
model.add(layers.Dense(16,activation=‘relu‘))
model.add(layers.Dense(1,activation=‘sigmoid‘))
model.compile(optimizer=‘rmsprop‘,loss=‘binary_crossentropy‘,metrics=[‘accuracy‘])
model.fit(x_train,y_train,epochs=4,batch_size=512)
results = model.evaluate(x_test,y_test)

binary_crossentropy 作为二分类的损失函数

metrics=[‘accuracy‘] 监控精度

optimizer=‘rmsprop‘ 作为优化器

import tensorflow as tf
import numpy as np
from keras.datasets import imdb
from keras import models
from keras import layers
def v(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1.
    return results
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
x_train = v(train_data)
x_test = v(test_data)
y_train = np.asarray(train_labels).astype(‘float32‘)
y_test = np.asarray(test_labels).astype(‘float32‘)
model = models.Sequential()
model.add(layers.Dense(16,activation=‘relu‘,input_shape=(10000,)))
model.add(layers.Dense(16,activation=‘relu‘))
model.add(layers.Dense(1,activation=‘sigmoid‘))
model.compile(optimizer=‘rmsprop‘,loss=‘mse‘,metrics=[‘accuracy‘])
model.fit(x_train,y_train,epochs=4,batch_size=512)
results = model.evaluate(x_test,y_test)

二分类问题

标签:oid   rms   Dimension   sha   model   load   精度   compile   sequence   

原文地址:https://www.cnblogs.com/strategist-614/p/12996588.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!