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

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

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

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

Q&A

解決済

1回答

1528閲覧

どうしてエラーが出るのかわかりません

iLia

総合スコア14

Python

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

0グッド

0クリップ

投稿2019/05/03 11:27

編集2019/05/03 23:58
# -*- coding: utf-8 -*- import keras from keras.datasets import mnist import matplotlib from matplotlib import pyplot import numpy as np from sklearn import datasets from sklearn.model_selection import cross_val_score as crv def sigmoid(x): return 1 / (1 + np.exp(-x)) def softmax(x): expX = np.exp(x) return expX / np.sum(expX) def cross_entropy_error(y,t): delta = 1e-7 batch_size = y.shape[0] idx= np.arange(batch_size) return -np.sum(np.log(y[idx,t]+delta)) / batch_size def gradient(f,x): if x.ndim == 1: return gradient_sub(f,x) else: grad = np.zeros_like(x) for index, xx in enumerate(x): grad[index] = gradient_sub(f,xx) return grad def gradient_sub(f,x): h = 1e-4 grad = np.zeros_like(x) for i in range(x.size): val = x[i] x[i] = val + h fx1 = f(x) x[i] = val - h fx2 = f(x) grad[i] = (fx1 - fx2) / (2*h) x[i] = val return grad class SampleNetwork: def __init__(self): input_size = 64 hidden_size = 50 output_size = 10 self.params = {} self.params["w0"] = 0.01 * np.random.randn(input_size,hidden_size) self.params["w1"] = 0.01 * np.random.randn(hidden_size,output_size) self.params["b0"] = np.zeros(hidden_size) self.params["b1"] = np.zeros(output_size) self.learning_rate = 0.1 def predict(self,x): a0 = np.dot(x,self.params["w0"]) + self.params["b0"] z0 = sigmoid(a0) a1 = np.dot(z0,self.params["w1"]) + self.params["b1"] y = softmax(a1) return y def updata_params(self,x,t): loss_W = lambda W: self.loss(x,t) for key in self.params.keys(): grad = gradient(loss_W, self.params[key]) self.params[key] -= self.learning_rate*grad def loss(self,x,t): y = self.predict(x) return cross_entropy_error(y,t) def accurary(self,x,t): y = self.predict(x) y = np.argmax(y,axis=1) acc = np.sum(y==t) / float(x.shape[0]) return acc digits = datasets.load_digits() X = digits.data T = digits.target (train_X, train_T), (test_X, test_T) = mnist.load_data() network = SampleNetwork() batch_size = 100 iter_num = 300 train_size = train_X.shape[0] erpoch_size = max(train_size//batch_size,1) loss_list = [] train_accurary_list = [] test_accurary_list = [] for index in range(iter_num): batch_choice = np.random.choice(train_size,batch_size) x_batch = train_X[batch_choice] t_batch = train_T[batch_choice] network.update_params(x_batch,t_batch) loss_list.append(loss) if (index % erpoch_size == 0): train_accurary = network.accurary(train_X,train_T) test_accurary = network.accurary(test_X,test_T) train_accurary_list.append(train_accurary) test_accurary_list.append(test_accurary) pyplot.figure(figsize=(10,7)) pyplot.subplot(2,2,1) pyplot.plot(np.arange(len(loss_list)),loss_list) pyplot.xlabel("iteration") pyplot.title("Cross Entropy Error") pyplot.subplot(2,2,2) pyplot.plot(np.arange(0,len(train_accurary_list),1),train_accurary_list,"b") pyplot.plot(np.arange(0,len(test_accurary_list),1),test_accurary_list,"ro") pyplot.xlabel("iteration(epoch)") pyplot.title("Accuary") pyplot.legend(("train","test"),loc = "lowrer right") pyplot.tight_layout()

#エラー
Using TensorFlow backend.
Traceback (most recent call last):
File "number.py", line 103, in <module>
network.update_params(x_batch,t_batch)
AttributeError: 'SampleNetwork' object has no attribute 'update_params'

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

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

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

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

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

y_waiwai

2019/05/03 11:29

このままではコードが読めないので、質門を編集し、<code>ボタンで、出てくる’’’の枠の中にコードを貼り付けてください
iLia

2019/05/03 11:32

わかりました。
iLia

2019/05/03 11:34

こういった形でよろしいでしょうか?
guest

回答1

0

ベストアンサー

AttributeError: 'SampleNetwork' object has no attribute 'update_params' 

というエラーが出ていて、

network.update_params(x_batch,t_batch)

network.accurary(test_X,test_T)

と記載して呼び出しているのであれば、

def predict(self,x):

def updata_params(self,x,t):

def loss(self,x,t):

def accurary(self,x,t):

がインデントが不足しているので、クラスのメソッドになっていないということではないでしょうか?

投稿2019/05/04 00:35

CHERRY

総合スコア25171

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

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

iLia

2019/05/04 08:40

":"この記号の次の行の前に一行開けるんですよね... それ以外にもインデントしないといけない部分があるのでしょうか?
iLia

2019/05/04 08:51

意味がわかりました。やってみます
iLia

2019/05/04 08:59

やってみたのですが、同じエラーが出てしまいます
iLia

2019/05/04 09:02

どうやら、私のスペルミスで、updateがupdataになっていました。それを直したのですが Using TensorFlow backend. Traceback (most recent call last): File "number.py", line 102, in <module> network.update_params(x_batch,t_batch) File "number.py", line 68, in update_params grad = gradient(loss_W, self.params[key]) File "number.py", line 29, in gradient grad[index] = gradient_sub(f,xx) File "number.py", line 38, in gradient_sub fx1 = f(x) File "number.py", line 66, in <lambda> loss_W = lambda W: self.loss(x,t) File "number.py", line 73, in loss y = self.predict(x) File "number.py", line 59, in predict a0 = np.dot(x,self.params["w0"]) + self.params["b0"] ValueError: shapes (100,28,28) and (64,50) not aligned: 28 (dim 2) != 64 (dim 0) とエラーが出てしまいました
iLia

2019/05/04 09:05

どこの部分の内積が違うのでしょうか
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問