import tensorflow as tf
mnist = tf.ist(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0model = dels.Sequential([tf.keras.layers.Flatten(input_shape=(28, 28)),tf.keras.layers.Dense(512, activationlu),tf.keras.layers.Dropout(0.2),tf.keras.layers.Dense(10, activationsoftmax)
])
modelpile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])model.fit(x_train, y_train, epochs=5)
model.evaluate(x_test, y_test)
这个本身精度不高,我们可以改变结构提升精度
from __future__ import division, print_function, absolute_import# Import MNIST data
ist import input_data
mnist = ad_data_sets("/tmp/data/", one_hot=False)import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np# Training Parameters
learning_rate = 0.001
num_steps = 2000
batch_size = 128# Network Parameters
num_input = 784 # MNIST data input (img shape: 28*28)
num_classes = 10 # MNIST total classes (0-9 digits)
dropout = 0.25 # Dropout, probability to drop a unit# Create the neural network
def conv_net(x_dict, n_classes, dropout, reuse, is_training):# Define a scope for reusing the variableswith tf.variable_scope('ConvNet', reuse=reuse):# TF Estimator input is a dict, in case of multiple inputsx = x_dict['images']# MNIST data input is a 1-D vector of 784 features (28*28 pixels)# Reshape to match picture format [Height x Width x Channel]# Tensor input become 4-D: [Batch Size, Height, Width, Channel]x = tf.reshape(x, shape=[-1, 28, 28, 1])# Convolution Layer with 32 filters and a kernel size of 5conv1 = v2d(x, 32, 5, activationlu)# Max Pooling (down-sampling) with strides of 2 and kernel size of 2conv1 = tf.layers.max_pooling2d(conv1, 2, 2)# Convolution Layer with 64 filters and a kernel size of 3conv2 = v2d(conv1, 64, 3, activationlu)# Max Pooling (down-sampling) with strides of 2 and kernel size of 2conv2 = tf.layers.max_pooling2d(conv2, 2, 2)# Flatten the data to a 1-D vector for the fully connected layerfc1 = tf.contrib.layers.flatten(conv2)# Fully connected layer (in tf contrib folder for now)fc1 = tf.layers.dense(fc1, 1024)# Apply Dropout (if is_training is False, dropout is not applied)fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)# Output layer, class predictionout = tf.layers.dense(fc1, n_classes)return out# Define the model function (following TF Estimator Template)
def model_fn(features, labels, mode):# Build the neural network# Because Dropout have different behavior at training and prediction time, we# need to create 2 distinct computation graphs that still share the same weights.logits_train = conv_net(features, num_classes, dropout, reuse=False, is_training=True)logits_test = conv_net(features, num_classes, dropout, reuse=True, is_training=False)# Predictionspred_classes = tf.argmax(logits_test, axis=1)pred_probas = tf.nn.softmax(logits_test)# If prediction mode, early returnif mode == tf.estimator.ModeKeys.PREDICT:return tf.estimator.EstimatorSpec(mode, predictions=pred_classes) # Define loss and optimizerloss_op = tf.reduce_sparse_softmax_cross_entropy_with_logits(logits=logits_train, labels=tf.cast(labels, dtype=tf.int32)))optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)train_op = optimizer.minimize(loss_op, global_step_global_step())# Evaluate the accuracy of the modelacc_op = tf.metrics.accuracy(labels=labels, predictions=pred_classes)# TF Estimators requires to return a EstimatorSpec, that specify# the different ops for training, evaluating, ...estim_specs = tf.estimator.EstimatorSpec(mode=mode,predictions=pred_classes,loss=loss_op,train_op=train_op,eval_metric_ops={'accuracy': acc_op})return estim_specs# Build the Estimator
model = tf.estimator.Estimator(model_fn)# Define the input function for training
input_fn = tf.estimator.inputs.numpy_input_fn(x={'images': ain.images}, yain.labels,batch_size=batch_size, num_epochs=None, shuffle=True)
# Train the Model
ain(input_fn, steps=num_steps)# Evaluate the Model
# Define the input function for evaluating
input_fn = tf.estimator.inputs.numpy_input_fn(x={'images': st.images}, yst.labels,batch_size=batch_size, shuffle=False)
# Use the Estimator 'evaluate' method
model.evaluate(input_fn)import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)#testad_csv('./input/test.csv')
import numpy
from numpy import genfromtxt
my_data = numpy.double(genfromtxt('./input/test.csv', delimiter=','))# Prepare the input data
input_fn = tf.estimator.inputs.numpy_input_fn(x={'images': numpy.float32(my_data[1:,:])}, shuffle=False)
# Use the model to predict the images class
preds2 = list(model.predict(input_fn))Submission = pd.DataFrame({"ImageId": range(1, len(preds2)+1),"Label": preds2})_csv("cnnMnistSubmission.csv", index=False)Submission.head(5)
本文发布于:2024-01-28 18:51:06,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/17064390719515.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |