質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

1回答

2432閲覧

Tensorflowのfully_connected_feedの実行にエラーが続く

退会済みユーザー

退会済みユーザー

総合スコア0

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2017/05/13 16:38

tensoflowのfully_connected_feed.pyを実行しようとしているのですが、どう試行錯誤してもエラーが続きます。
tensorflowのインストールの仕方、実行する場所に問題があるのかもしれないので、手順を書きます。

http://qiita.com/hatapu/items/054dbab03607c47cb84f
http://d.hatena.ne.jp/shu223/20160105/1451952796#20160105f1
を参考にしました。
1

$ brew install pyenv-virtualenv
pyenv virtualenv 3.5.0 py350tensorflow

2

$ pyenv version py350tensorflow (set by /Users/cloudspider/.pyenv/version)

こんな感じで表示されました。

3

$ sudo pip install --upgrade virtualenv $ virtualenv --system-site-packages ~/tensorflow $ source ~/tensorflow/bin/activate pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.6.0-py3-none-any.whl

4
ホームディレクトリに「tensorflow」というディレクトリができたので中に入りgitという名前のディレクトリを作り、その中に入ります。

$cd tensorflow $mkdir git $cd git

5
ここでtensorflowのcloneをしました。

$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow

6
fully_connected_feed.py の30,31行目を修正をしました。

python

1import input_data 2import mnist

7
実行しました。(アクティベートされている状態です)

$ 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'

cloneするものをr0.8にしたり、tensorflowというディレクトリ名が被るのでダメなのかと思い、デスクトップに移動してみたり色々試したのですが、ダメでした。

そもそも /tensorflow/git/tensorflowで実行している事自体は良いのか。
/.pyenv/versions/py350tensorflowの中じゃないとダメじゃないのか。
source ~/tensorflow/bin/activateでアクティベートしている状態で実行してもよいのか。
そもそもこのアクティベートは何の意味があるのか。
など疑問が疑問を呼ぶ状態です。
エラー文にある通りにディレクトリを辿ると確かにそのディレクトリが存在しているのにエラーが出続きます。

どこに問題があるのでしょうか。
よろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

インストールの仕方やtensorflowのバージョンが古いようですね。
今では、コマンド一発でインストールできるようになっています。

GPU環境の場合

sh

1pip install tensorflow-gpu

CPU環境の場合

sh

1pip install tensorflow

上を実行した上で、以下のサンプルコードを動かしてみてください。

python

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 at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# 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 and 13# 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""" 18 19from __future__ import absolute_import 20from __future__ import division 21from __future__ import print_function 22 23import numpy as np 24from sklearn import metrics 25import tensorflow as tf 26 27layers = tf.contrib.layers 28learn = tf.contrib.learn 29 30 31def max_pool_2x2(tensor_in): 32 return tf.nn.max_pool( 33 tensor_in, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 34 35 36def conv_model(feature, target, mode): 37 """2-layer convolution model.""" 38 # Convert the target to a one-hot tensor of shape (batch_size, 10) and 39 # 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) 41 42 # Reshape feature to 4d tensor with 2nd and 3rd dimensions being 43 # image width and height final dimension being the number of color channels. 44 feature = tf.reshape(feature, [-1, 28, 28, 1]) 45 46 # First conv layer will compute 32 features for each 5x5 patch 47 with 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) 51 52 # Second conv layer will compute 64 features for each 5x5 patch. 53 with 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 vectors 58 h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64]) 59 60 # 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) 66 67 # 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) 70 71 # 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) 77 78 return tf.argmax(logits, 1), loss, train_op 79 80 81def main(unused_args): 82 ### Download and load MNIST dataset. 83 mnist = learn.datasets.load_dataset('mnist') 84 85 ### 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, 95 list(classifier.predict(mnist.test.images))) 96 print('Accuracy: {0:f}'.format(score)) 97 98 ### Convolutional network 99 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, 105 list(classifier.predict(mnist.test.images))) 106 print('Accuracy: {0:f}'.format(score)) 107 108 109if __name__ == '__main__': 110tf.app.run()

投稿2017/05/14 01:52

MasashiKimura

総合スコア1150

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問