keras 实现线性模型
In [1]:
from utils import *
from keras.optimizers import SGD, RMSprop, Adam
In [2]:
x = random((30, 2))
x[:3]
Out[2]:
In [3]:
y = x.dot([2., 3.]) + 1.
y[:3]
Out[3]:
In [4]:
lm = Sequential([Dense(1, input_shape=(2,))])
lm.compile(optimizer=SGD(lr=.1), loss='mse')
In [5]:
lm.fit(x, y, nb_epoch=10, batch_size=1)
Out[5]:
In [6]:
lm.get_weights()
Out[6]:
VGG change
In [7]:
import vgg16
from vgg16 import Vgg16
In [ ]:
vgg = Vgg16()
In [ ]:
path = 'data/redux/'
# path = 'data/redux/sample/'
model_path = path + 'models/'
In [ ]:
In [ ]:
BATCH_SIZE = 8
In [ ]:
val_batches = get_batches(path + 'valid', shuffle=False, batch_size=BATCH_SIZE)
batches = get_batches(path + 'train', shuffle=False, batch_size=BATCH_SIZE)
In [ ]:
#batches.nb_sample
#batches.next()
In [ ]:
import bcolz
def save_array(fname, arr): c=bcolz.carray(arr, rootdir=fname, mode='w'); c.flush()
def load_array(fname): return bcolz.open(fname)[:]
def onehot(x): return np.array(OneHotEncoder().fit_transform(x.reshape(-1,1)).todense())
In [ ]:
# val_batches
In [ ]:
# val_data = get_data(path + 'valid')
In [ ]:
# trn_data = get_data(path + 'train')
In [ ]:
# save_array(model_path + 'train_data.bc', trn_data)
# save_array(model_path + 'valid_data.bc', val_data)
In [ ]:
# trn_data = load_array(model_path + 'train_data.bc')
# val_data = load_array(model_path + 'valid_data.bc')
In [ ]:
# trn_data.shape
In [ ]:
val_classes = val_batches.classes
trn_classes = batches.classes
val_labels = onehot(val_classes)
trn_labels = onehot(trn_classes)
In [ ]:
trn_labels.shape
In [ ]:
# trn_features = model.predict(trn_data, batch_size=BATCH_SIZE)
# trn_features
In [ ]:
# val_features = model.predict(val_data, batch_size=BATCH_SIZE)
# val_features
In [ ]:
val_features = model.predict_generator(val_batches, val_batches.nb_sample)
In [ ]:
val_features.shape
In [ ]:
trn_features = model.predict_generator(batches, batches.nb_sample)
In [ ]:
trn_features
In [ ]:
# vgg.compile()
# vgg.fit(batches, val_batches, nb_epoch=1)
In [ ]:
save_array(model_path + 'train_lastlayer_features.bc', trn_features)
save_array(model_path + 'valid_lastlayer_features.bc', val_features)
In [ ]:
trn_features = load_array(model_path + 'train_lastlayer_features.bc')
val_features = load_array(model_path + 'valid_lastlayer_features.bc')
In [ ]:
lm = Sequential([Dense(2, activation='softmax', input_shape=(1000, ))])
lm.compile(optimizer=RMSprop(lr=.1), loss='categorical_crossentropy', metrics=['accuracy'])
In [25]:
lm.fit(trn_features, trn_labels, nb_epoch=3, batch_size=BATCH_SIZE, validation_data=(val_features, val_labels))
Out[25]:
In [26]:
lm.fit(trn_features, trn_labels, nb_epoch=6, batch_size=BATCH_SIZE, validation_data=(val_features, val_labels))
Out[26]:
In [1]:
lm.summary()
In [ ]: