zoukankan      html  css  js  c++  java
  • anaconda+theano+keras手写字符识别新版

    标题介绍运行环境了win7            

    看网上好多keras识别minist 但是一般由于版本问题,无法直接用,,,这里还要特别感谢keras中文文档作者(三当家SCP)。教程整的非常好。还有就是最好你在安装anaconda 之前把原来安装过的PY卸载掉,要不然安装mingw的时候会出问题,,,安装就不详细介绍了网上有很多种----大致流程——anaconda-mingw-theano(注意环境变量,系统变量啥的)-keras。

    下边附上一个可用程序哈,亲测可用。。。并附上数据,数据来源于网络,见文章底部,你就不用运行的时候在下载数据,这样很容易出错的。非GPU环境 win7 64bit

    好的:其实重点是新的语法的问题。。。。。。。。。。。。。。。。。。。更新地方已标红。。。。。。。

     1 from __future__ import absolute_import
     2 from __future__ import print_function
     3 import numpy as np
     4 np.random.seed(1337)  # for reproducibility
     5 import cPickle as pickle  
     6 
     7 from keras.models import Sequential
     8 from keras.layers.core import Dense, Dropout, Activation
     9 from keras.optimizers import SGD, Adam, RMSprop
    10 from keras.utils import np_utils
    11 
    12 '''
    13     Train a simple deep NN on the MNIST dataset.
    14     Get to 98.30% test accuracy after 20 epochs (there is *a lot* of margin for parameter tuning).
    15     2 seconds per epoch on a GRID K520 GPU.
    16 '''
    17 
    18 batch_size = 128
    19 nb_classes = 10
    20 nb_epoch = 10
    21 
    22 
    23 def read_data(data_file):  
    24     import gzip  
    25     f = gzip.open(data_file, "rb")  
    26     train, val, test = pickle.load(f)  
    27     f.close()  
    28     train_x = train[0]  
    29     train_y = train[1]  
    30     test_x = test[0]  
    31     test_y = test[1]  
    32     return train_x, train_y, test_x, test_y  
    33     
    34     
    35 # the data, shuffled and split between tran and test sets
    36 #(X_train, y_train), (X_test, y_test) = mnist.load_data()
    37 train_x, train_y, test_x, test_y = read_data("C:UsersPC.spyder2mnist.pkl.gz")  
    38 X_train = train_x
    39 X_test = test_x
    40 X_train = X_train.astype("float32")
    41 X_test = X_test.astype("float32")
    42 X_train /= 255
    43 X_test /= 255
    44 print(X_train.shape[0], 'train samples')
    45 print(X_test.shape[0], 'test samples')
    46 
    47 # convert class vectors to binary class matrices
    48 Y_train = np_utils.to_categorical(train_y, nb_classes)
    49 Y_test = np_utils.to_categorical(test_y, nb_classes)
    50 
    51 model = Sequential()
    52 model.add(Dense(input_dim=784, output_dim=128))
    53 model.add(Activation('relu'))
    54 model.add(Dropout(0.2))
    55 model.add(Dense(output_dim=128))
    56 model.add(Activation('relu'))
    57 model.add(Dropout(0.2))
    58 model.add(Dense(output_dim=10))
    59 model.add(Activation('softmax'))
    60 
    61 rms = RMSprop()
    62 model.compile(loss='categorical_crossentropy', optimizer=rms,metrics=['accuracy'])
    63 
    64 model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch)
    65 score = model.evaluate(X_test, Y_test, batch_size=batch_size)
    66 print('Test score:', score[0])
    67 print('Test accuracy:', score[1])

    数据在这里:http://www.cnblogs.com/xueliangliu/archive/2013/04/03/2997437.html。。。由于没法上传有15兆,我看这个大牛有这个数据,而且还附带了安装及原理等,自己看吧。。。。。。。

  • 相关阅读:
    html5页面资源预加载(Link prefetch)
    html5页面资源预加载(Link prefetch)
    纯CSS制作的图形效果
    echarts 较全面的参数设置分析
    设置css样式背景色透明 字体颜色的不透明 设置select 箭头样式
    this.refs['hh']获取dom对象,this.refs['hh'].value获取dom对象的值
    浏览器运行的时候 事件打印不出来,提示 此页面出现代码禁用了反向和正向缓存(由于默认事件导致的)
    react 点击事件以及原始event与react封装好的事件点击区别
    react中 props与forEach的用法
    基于webpack的react的环境项目搭建
  • 原文地址:https://www.cnblogs.com/8335IT/p/5701061.html
Copyright © 2011-2022 走看看