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

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

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

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

598閲覧

mnist for ML Beginnersについて

ruuruusann24

総合スコア16

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2018/01/21 18:47

https://qiita.com/kkk3H/items/c3eb0d868170b29b0b87

こちらの記事を読ませていただいて自分も同じことをしてみたいと思ってやってみたのですが、うまく行きませんでした。
最初の
data = data_set.read_data_sets(one_hot=True)
でエラーが出てしまっていると思うのですがなぜエラーが出てしまうのかわかりません。
よろしくお願いします。

Jupyter Notebook
Python 3.6.4
TensorFlow 1.3.0
NumPy 1.13.3

python

1import tensorflow as tf 2import dataset 3 4data = data_set.read_data_sets(one_hot=True) 5 6x = tf.placeholder(tf.float32, [None, 784]) 7y_ = tf.placeholder(tf.float32, [None, 3]) 8 9W = tf.Variable(tf.zeros([784, 3])) 10b = tf.Variable(tf.zeros([3])) 11 12y = tf.nn.softmax(tf.matmul(x, W) + b) 13 14cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) 15 16train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) 17 18init = tf.initialize_all_variables() 19sess = tf.Session() 20sess.run(init) 21 22for i in range(100): 23 batch_xs, batch_ys = data.train.next_batch(10) 24 sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) 25 26correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 27accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) 28 29sess.run(accuracy, feed_dict={x: data.test.images, y_: data.test.labels}) 30

python

1"data_set.py" 2import numpy 3from tensorflow.python.framework import dtypes 4import collections 5 6Datasets = collections.namedtuple('datasets', ['train', 'validation', 'test']) 7 8 9def dense_to_one_hot(labels_dense, num_classes): 10 num_labels = labels_dense.shape[0] 11 index_offset = numpy.arange(num_labels) * num_classes 12 labels_one_hot = numpy.zeros((num_labels, num_classes)) 13 labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1 14 15 return labels_one_hot 16 17 18class DataSet(object): 19 20 def __init__(self, 21 images, 22 labels): 23 24 self._images = images 25 self._labels = labels 26 self._num_examples = images.shape[0] 27 self._epochs_completed = 0 28 self._index_in_epoch = 0 29 30 self._epoch_test = 0 31 self._index_in_epoch_test = 0 32 33 @property 34 def images(self): 35 return self._images 36 37 @property 38 def labels(self): 39 return self._labels 40 41 @property 42 def num_examples(self): 43 return self._num_examples 44 45 @property 46 def epochs_completed(self): 47 return self._epochs_completed 48 49 @property 50 def epoch_test(self): 51 return self._epoch_test 52 53 def next_batch(self, batch_size, shuffle=True): 54 start = self._index_in_epoch 55 # Shuffle for the first epoch 56 if self._epochs_completed == 0 and start == 0 and shuffle: 57 perm0 = numpy.arange(self._num_examples) 58 numpy.random.shuffle(perm0) 59 self._images = self.images[perm0] 60 self._labels = self.labels[perm0] 61 # Go the the next epoch 62 if start + batch_size > self._num_examples: 63 # Finished epoch 64 self._epochs_completed += 1 65 # Get the rest examples in this epoch 66 rest_num_examples = self._num_examples - start 67 data_rest_part = self._images[start:self._num_examples] 68 label_rest_part = self._labels[start:self._num_examples] 69 # Shuffle the data 70 if shuffle: 71 perm = numpy.arange(self._num_examples) 72 numpy.random.shuffle(perm) 73 self._images = self.images[perm] 74 self._labels = self.labels[perm] 75 # Start next epoch 76 start = 0 77 self._index_in_epoch = batch_size - rest_num_examples 78 end = self._index_in_epoch 79 data_new_part = self._images[start:end] 80 label_new_part = self._labels[start:end] 81 82 return numpy.concatenate((data_rest_part, data_new_part), axis = 0), numpy.concatenate((label_rest_part, label_new_part), axis = 0) 83 else: 84 self._index_in_epoch += batch_size 85 end = self._index_in_epoch 86 return self._images[start:end], self._labels[start:end] 87 88def read_data_sets(one_hot=False, 89 dtype=dtypes.float32, 90 reshape=True, 91 validation_size=1000, 92 test_size=500): 93 """Construct a data set from a given directory path of the data""" 94 95 images = numpy.load('./imgs.npy') 96 labels = numpy.load('./labels.npy') 97 98 if reshape: 99 images = numpy.reshape(images, (-1, numpy.prod(images.shape[1:]))) 100 if one_hot: 101 labels = dense_to_one_hot(labels, 3) 102 103 validation_images = images[:validation_size] 104 validation_labels = labels[:validation_size] 105 test_images = images[validation_size:validation_size+test_size] 106 test_labels = labels[validation_size:validation_size+test_size] 107 train_images = images[validation_size+test_size:] 108 train_labels = labels[validation_size+test_size:] 109 110 train = DataSet(train_images, 111 train_labels) 112 113 validation = DataSet(validation_images, 114 validation_labels) 115 116 test = DataSet(test_images, 117 test_labels) 118 119 return Datasets(train=train, validation=validation, test=test) 120 121

以下エラー文です

--------------------------------------------------------------------------- TypeError Traceback (most recent call last) ~/anaconda3/envs/tf/lib/python3.6/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds) 56 try: ---> 57 return getattr(obj, method)(*args, **kwds) 58 TypeError: 'numpy.float64' object cannot be interpreted as an integer During handling of the above exception, another exception occurred: TypeError Traceback (most recent call last) <ipython-input-23-9ceed1aa7dd7> in <module>() 2 import dataset 3 ----> 4 data = data_set.read_data_sets(one_hot=True) ~/lab/data_set/data_set.py in read_data_sets(one_hot, dtype, reshape, validation_size, test_size) 96 97 if reshape: ---> 98 images = numpy.reshape(images, (-1, numpy.prod(images.shape[1:]))) 99 if one_hot: 100 labels = dense_to_one_hot(labels, 3) ~/anaconda3/envs/tf/lib/python3.6/site-packages/numpy/core/fromnumeric.py in reshape(a, newshape, order) 230 [5, 6]]) 231 """ --> 232 return _wrapfunc(a, 'reshape', newshape, order=order) 233 234 ~/anaconda3/envs/tf/lib/python3.6/site-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds) 65 # a downstream library like 'pandas'. 66 except (AttributeError, TypeError): ---> 67 return _wrapit(obj, method, *args, **kwds) 68 69 ~/anaconda3/envs/tf/lib/python3.6/site-packages/numpy/core/fromnumeric.py in _wrapit(obj, method, *args, **kwds) 45 except AttributeError: 46 wrap = None ---> 47 result = getattr(asarray(obj), method)(*args, **kwds) 48 if wrap: 49 if not isinstance(result, mu.ndarray): TypeError: 'numpy.float64' object cannot be interpreted as an integer

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

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

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

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

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

guest

回答2

0

ベストアンサー

Python

1images = numpy.reshape(images, (-1, numpy.prod(images.shape[1:])))

Errorを見るとこの部分がおかしいんだと思います。
内容としては、numpy.reshapeの引数にはint型しか受け付けないようになっているのに、numpy.float64型が入っているからでしょう。images.shape[1:]の中身が分からないので何とも言えませんが、prodの計算でimage[1:]の中にfloat型が一つでも入っていたら返り値はnumpy.float64になるようなので、データの中身を洗ってみてはいかがでしょう。

投稿2018/01/22 01:44

Yuki_S

総合スコア356

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

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

ruuruusann24

2018/01/22 03:05

回答ありがとうございます!確かにデータの中を調べてみたところデータがおかしかったです。解決しました!ありがとうございます!
guest

0

回答ありがとうございます!確かにデータの中を調べてみたところデータがおかしかったです。解決しました!ありがとうございます!

投稿2018/01/22 03:04

ruuruusann24

総合スコア16

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問