python
1# リスト 7-2-(1) 2import numpy as np 3import matplotlib.pyplot as plt 4import time 5np.random.seed(1) # (A) 6import keras.optimizers # (B) 7from keras.models import Sequential # (C) 8from keras.layers.core import Dense, Activation #(D) 9 10# データの load --------------------------- 11outfile = np.load('class_data.npz') 12X_train = outfile['X_train'] 13T_train = outfile['T_train'] 14X_test = outfile['X_test'] 15T_test = outfile['T_test'] 16X_range0 = outfile['X_range0'] 17X_range1 = outfile['X_range1'] 18# リスト 7-2-(2) 19# データの図示 ------------------------------ 20def Show_data(x, t): 21 wk, n = t.shape 22 c = [[0, 0, 0], [.5, .5, .5], [1, 1, 1]] 23 for i in range(n): 24 plt.plot(x[t[:, i] == 1, 0], x[t[:, i] == 1, 1], 25 linestyle='none', marker='o', 26 markeredgecolor='black', 27 color=c[i], alpha=0.8) 28 plt.grid(True) 29# リスト 7-2-(3) 30 31# 乱数の初期化 32np.random.seed(1) 33 34# --- Sequential モデルの作成 35model = Sequential() 36model.add(Dense(2, input_dim=2, activation='sigmoid', 37 kernel_initializer='uniform')) # (A) 38model.add(Dense(3,activation='softmax', 39 kernel_initializer='uniform')) # (B) 40sgd = keras.optimizers.SGD(lr=0.5, momentum=0.0, 41 decay=0.0, nesterov=False) # (C) 42model.compile(optimizer=sgd, loss='categorical_crossentropy', 43 metrics=['accuracy']) # (D) 44 45# ---------- 学習 46startTime = time.time() 47history = model.fit(X_train, T_train, epochs=1000, batch_size=100, 48 verbose=0, validation_data=(X_test, T_test)) # (E) 49 50# ---------- モデル評価 51score = model.evaluate(X_test, T_test, verbose=0) # (F) 52print('cross entropy {0:.2f}, accuracy {1:.2f}'\ 53 .format(score[0], score[1])) 54calculation_time = time.time() - startTime 55print("Calculation time:{0:.3f} sec".format(calculation_time))
と入力して、実行すると、以下のエラーが出て動きません。
Keras のバージョンは2.2.4
AttributeError Traceback (most recent call last)
<ipython-input-19-5de401a576da> in <module>
5
6 # --- Sequential モデルの作成
----> 7 model = Sequential()
8 model.add(Dense(2, input_dim=2, activation='sigmoid',
9 kernel_initializer='uniform')) # (A)
~\anaconda3\lib\site-packages\keras\engine\sequential.py in init(self, layers, name)
85
86 def init(self, layers=None, name=None):
---> 87 super(Sequential, self).init(name=name)
88 self._build_input_shape = None
89
~\anaconda3\lib\site-packages\keras\legacy\interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your ' + object_name + '
call to the ' +
90 'Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
~\anaconda3\lib\site-packages\keras\engine\network.py in init(self, *args, **kwargs)
94 else:
95 # Subclassed network
---> 96 self._init_subclassed_network(**kwargs)
97
98 def _base_init(self, name=None):
~\anaconda3\lib\site-packages\keras\engine\network.py in _init_subclassed_network(self, name)
292
293 def _init_subclassed_network(self, name=None):
--> 294 self._base_init(name=name)
295 self._is_graph_network = False
296 self._expects_training_arg = has_arg(self.call, 'training')
~\anaconda3\lib\site-packages\keras\engine\network.py in base_init(self, name)
107 if not name:
108 prefix = self.class.name.lower()
--> 109 name = prefix + '' + str(K.get_uid(prefix))
110 self.name = name
111
~\anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in get_uid(prefix)
72 """
73 global _GRAPH_UID_DICTS
---> 74 graph = tf.get_default_graph()
75 if graph not in _GRAPH_UID_DICTS:
76 _GRAPH_UID_DICTS[graph] = defaultdict(int)
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
回答1件
あなたの回答
tips
プレビュー