要求
- pycharm 要求是专业版,不能是社区版(网上这么说的,我也没试过社区版行不行)
- 针对我的服务器,没有管理员去权限的小天。py版本为3.5和2.7,在Py3.5下的虚拟环境Py3下安装了tensorflow和keras.
要点
- 选择Interpreter 为py3/bin/python 这样就可以使用服务器的资源了,
- local文件需要upload到服务器上进行远程调试
第一个测试
没啥装啥: pip install …
# -*- coding: utf-8 -*-
__author__ = "TF大Q"
import numpy as np
np.random.seed(1337)
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
# 全局变量
batch_size = 128
nb_classes = 10
epochs = 5
# input image dimensions
img_rows, img_cols = 28, 28
# 卷积滤波器的数量
nb_filters = 32
# size of pooling area for max pooling
pool_size = (2, 2)
# convolution kernel size
kernel_size = (3, 3)
# load data
(X_train, y_train), (X_test, y_test) = mnist.load_data()
from sklearn.model_selection import train_test_split
# x为数据集的feature熟悉,y为label.
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size=0.2)
# 根据不同的backend定下不同的格式
if K.image_dim_ordering() == 'th':
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
X_valid = X_valid.reshape(X_valid.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1)
X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1)
X_valid = X_valid.reshape(X_valid.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
# 类型转换
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_valid = X_valid.astype('float32')
X_train /= 255
X_test /= 255
X_valid /= 255
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
print(X_valid.shape[0], 'valid samples')
# 转换为one_hot类型
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
Y_valid = np_utils.to_categorical(y_valid, nb_classes)
# 构建模型
model = Sequential()
model.add(Convolution2D(nb_filters, (kernel_size[0], kernel_size[1]),
padding='same',
input_shape=input_shape)) # 卷积层1
model.add(Activation('relu')) # 激活层
model.add(Convolution2D(nb_filters, (kernel_size[0], kernel_size[1]))) # 卷积层2
model.add(Activation('relu')) # 激活层
model.add(MaxPooling2D(pool_size=pool_size)) # 池化层
model.add(Dropout(0.25)) # 神经元随机失活
model.add(Flatten()) # 拉成一维数据
model.add(Dense(128)) # 全连接层1
model.add(Activation('relu')) # 激活层
model.add(Dropout(0.5)) # 随机失活
model.add(Dense(nb_classes)) # 全连接层2
model.add(Activation('softmax')) # Softmax评分
# 编译模型
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
# 训练模型
model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs,
verbose=1, validation_data=(X_valid, Y_valid))
# 评估模型
score = model.evaluate(X_test, Y_test, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])