初心者の質問となります。ご容赦ください。
今回、趣味で初めてAIのプログラミングを作成しようと思い、とあるサイトを拝見しながら進めておりました。
そこで、以下のようなメッセージが発生し、混乱しております。
C:\Users\コンピュータ名\Documents\K>python stock_ai.py
2021-12-15 21:50:07.418432: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-12-15 21:50:08.902574: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1525] Created device /job:localhost/replica:0/task:0/device:GPU:0 with 2151 MB memory: -> device: 0, name: NVIDIA GeForce GTX 1650, pci bus id: 0000:01:00.0, compute capability: 7.5
Traceback (most recent call last):
File "stock_ai.py", line 51, in <module>
optimizer=optimizers.Adam(lr=1e-4),
AttributeError: module 'keras.optimizers' has no attribute 'Adam'
該当のソースコード
from keras import layers
from keras import models
from keras import optimizers
from keras.callbacks import ModelCheckpoint
from keras.preprocessing.image import ImageDataGenerator
import matplotlib.pyplot as plt
import os
import math
data_dir = 'data/chart' #データフォルダの名前
base_dir = "data/" #ログ出力用
def_batch_size = 1 #バッチサイズ
def weight(classes_name, dir_name):
data_element_num = {}
max_buf = 0
for class_name in classes_name:
class_dir = dir_name + os.sep + class_name
files = os.listdir(class_dir)
data_element_num[class_name] = len(files)
if max_buf < len(files):
max_buf = len(files)
weights = {}
count = 0
for class_name in classes_name:
weights[count] = round(float(math.pow(data_element_num[class_name]/max_buf, -1)), 2)
count = count + 1
return weights
model = models.Sequential()
model.add(layers.Conv2D(32, (3, 3), activation='relu',
input_shape=(256, 256, 1)))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Conv2D(64, (3, 3), activation='relu'))
model.add(layers.Conv2D(128, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Dropout(0.5))
model.add(layers.Conv2D(256, (3, 3), activation='relu'))
model.add(layers.Conv2D(512, (3, 3), activation='relu'))
model.add(layers.MaxPooling2D((2, 2)))
model.add(layers.Dropout(0.5))
model.add(layers.Conv2D(512, (3, 3), activation='relu'))
model.add(layers.Flatten())
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer=optimizers.Adam(lr=1e-4),
metrics=['acc'])
datagen = ImageDataGenerator(validation_split=0.3, rescale=1./255)
train_generator = datagen.flow_from_directory(
data_dir,
batch_size=def_batch_size,
class_mode='binary',
target_size=(256, 256),
color_mode='grayscale',
subset='training')
validation_generator = datagen.flow_from_directory(
data_dir,
batch_size=def_batch_size,
class_mode='binary',
target_size=(256, 256),
color_mode='grayscale',
subset='validation')
for data_batch, labels_batch in train_generator:
print('data batch shape:', data_batch.shape)
print('labels batch shape:', labels_batch.shape)
break
fpath = base_dir + 'chart.{epoch:02d}.h5'
modelCheckpoint = ModelCheckpoint(filepath = fpath,
monitor='val_loss',
verbose=1,
save_best_only=True,
save_weights_only=False,
mode='min',
save_freq='epoch')
class_weights = weight(classes_name = ['down', 'up'], dir_name = data_dir)
print('class weight:', class_weights)
history = model.fit(
train_generator,
steps_per_epoch=train_generator.samples // def_batch_size,
validation_data = validation_generator,
epochs = 100,
validation_steps=validation_generator.samples // def_batch_size,
class_weight=class_weights,
callbacks=[modelCheckpoint])
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
fig = plt.figure()
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.savefig(base_dir + 'accuracy.png')
plt.close()
fig = plt.figure()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'b', label='Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.savefig(base_dir + 'loss.png')
plt.close()
試したこと
同様のメッセージが出ているかの検索
→解決には至らず
補足情報(FW/ツールのバージョンなど)
Winows 11
python --version: 3.8.6
CUDA 11.5
TensorFlow 2.7.0
NVIDIA グラフィックスドライバー 497.09
稚拙な分になり申し訳ないです。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/12/16 12:25
2021/12/16 12:28