$ cd tensorflow/
$ python tensorflow/examples/tutorials/mnist/fully_connected_feed.py
8
エラーが出ます
Traceback (most recent call last):
File "tensorflow/examples/tutorials/mnist/fully_connected_feed.py", line 32, in <module>
import input_data
File "/Users/cloudspider/tensorflow/git/tensorflow/tensorflow/examples/tutorials/mnist/input_data.py", line 29, in <module>
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
ImportError: No module named 'tensorflow.contrib'
1# Copyright 2016 The TensorFlow Authors. All Rights Reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14"""This showcases how simple it is to build image classification networks.
15It follows description from this TensorFlow tutorial:
16 https://www.tensorflow.org/versions/master/tutorials/mnist/pros/index.html#deep-mnist-for-experts
17"""1819from __future__ import absolute_import
20from __future__ import division
21from __future__ import print_function
2223import numpy as np
24from sklearn import metrics
25import tensorflow as tf
2627layers = tf.contrib.layers
28learn = tf.contrib.learn
293031defmax_pool_2x2(tensor_in):32return tf.nn.max_pool(33 tensor_in, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')343536defconv_model(feature, target, mode):37"""2-layer convolution model."""38# Convert the target to a one-hot tensor of shape (batch_size, 10) and39# with a on-value of 1 for each one-hot vector of length 10.40 target = tf.one_hot(tf.cast(target, tf.int32),10,1,0)4142# Reshape feature to 4d tensor with 2nd and 3rd dimensions being43# image width and height final dimension being the number of color channels.44 feature = tf.reshape(feature,[-1,28,28,1])4546# First conv layer will compute 32 features for each 5x5 patch47with tf.variable_scope('conv_layer1'):48 h_conv1 = layers.convolution2d(49 feature,32, kernel_size=[5,5], activation_fn=tf.nn.relu)50 h_pool1 = max_pool_2x2(h_conv1)5152# Second conv layer will compute 64 features for each 5x5 patch.53with tf.variable_scope('conv_layer2'):54 h_conv2 = layers.convolution2d(55 h_pool1,64, kernel_size=[5,5], activation_fn=tf.nn.relu)56 h_pool2 = max_pool_2x2(h_conv2)57# reshape tensor into a batch of vectors58 h_pool2_flat = tf.reshape(h_pool2,[-1,7*7*64])5960# Densely connected layer with 1024 neurons.61 h_fc1 = layers.dropout(62 layers.fully_connected(63 h_pool2_flat,1024, activation_fn=tf.nn.relu),64 keep_prob=0.5,65 is_training=mode == tf.contrib.learn.ModeKeys.TRAIN)6667# Compute logits (1 per class) and compute loss.68 logits = layers.fully_connected(h_fc1,10, activation_fn=None)69 loss = tf.losses.softmax_cross_entropy(target, logits)7071# Create a tensor for training op.72 train_op = layers.optimize_loss(73 loss,74 tf.contrib.framework.get_global_step(),75 optimizer='SGD',76 learning_rate=0.001)7778return tf.argmax(logits,1), loss, train_op
798081defmain(unused_args):82### Download and load MNIST dataset.83 mnist = learn.datasets.load_dataset('mnist')8485### Linear classifier.86 feature_columns = learn.infer_real_valued_columns_from_input(87 mnist.train.images)88 classifier = learn.LinearClassifier(89 feature_columns=feature_columns, n_classes=10)90 classifier.fit(mnist.train.images,91 mnist.train.labels.astype(np.int32),92 batch_size=100,93 steps=1000)94 score = metrics.accuracy_score(mnist.test.labels,95list(classifier.predict(mnist.test.images)))96print('Accuracy: {0:f}'.format(score))9798### Convolutional network99 classifier = learn.Estimator(model_fn=conv_model)100 classifier.fit(mnist.train.images,101 mnist.train.labels,102 batch_size=100,103 steps=20000)104 score = metrics.accuracy_score(mnist.test.labels,105list(classifier.predict(mnist.test.images)))106print('Accuracy: {0:f}'.format(score))107108109if __name__ =='__main__':110tf.app.run()
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。