以下提到的问题的可能解决方案是什么?

时间:2019-11-21 10:21:14

标签: arrays python-3.x numpy tensorflow conv-neural-network

我正在使用Tensorflow进行课程设置的神经网络作业,我的准确度是99.9%,但是当我提交作业时,系统生成注释“您的返回类型不是np.float32”,请告诉我这是什么。问题是,我该如何解决?

import tensorflow as tf
import os
import zipfile
from os import path, getcwd, chdir
path = f"{getcwd()}/../tmp2/happy-or-sad.zip"
zip_ref = zipfile.ZipFile(path, 'r')
zip_ref.extractall("/tmp/h-or-s")
zip_ref.close()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
# Directory with our training happy pictures
train_happy_dir = os.path.join('/tmp/happy-or-sad/happy')

# Directory with our training sad pictures
train_sad_dir = os.path.join('/tmp/happy-or-sad/sad')

# Directory with our training happy pictures
validation_happy_dir = os.path.join('/tmp/h-or-s/validation-happy')

# Directory with our training sad pictures
validation_sad_dir = os.path.join('/tmp/h-or-s/validation-sad')

import numpy as np
class myCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self,epoch,logs={}):
        if(logs.get('acc')>0.999):
            print("\nReached 99.9% accuracy so cancelling training!")
            self.model.stop_training=True
# GRADED FUNCTION: train_happy_sad_model
def train_happy_sad_model():

    model= tf.keras.models.Sequential([
        tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150,150,3)),
        tf.keras.layers.MaxPooling2D(2,2),
        tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
        tf.keras.layers.MaxPooling2D(2,2),
        tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
        tf.keras.layers.MaxPooling2D(2,2),
        tf.keras.layers.Flatten(),
        tf.keras.layers.Dense(512, activation='relu'),
        tf.keras.layers.Dense(1, activation='sigmoid')
    ])
    model.summary()
    from tensorflow.keras.optimizers import RMSprop
    model.compile(loss='binary_crossentropy',
              optimizer=RMSprop(lr=0.001, rho=0.9, epsilon=None, decay=0.0),
              metrics=['acc'])
    from tensorflow.keras.preprocessing.image import ImageDataGenerator
    train_datagen = ImageDataGenerator(rescale=1/255)
    validation_datagen = ImageDataGenerator(rescale=1/255)
    train_generator = train_datagen.flow_from_directory('/tmp/h-or-s', target_size=(150, 150),
                                                        batch_size=128, class_mode='binary')
    validation_generator = validation_datagen.flow_from_directory('/tmp/h-or-s', target_size=(150, 150),
                                                                  batch_size=32, class_mode='binary')
    callbacks=myCallback()

    history = model.fit_generator(
      train_generator,
      steps_per_epoch=8,  
      epochs=15,verbose=1,
      validation_data = validation_generator,
      validation_steps=8,
      callbacks=[callbacks])
    return np.float32(history.epoch),history.history['acc'][-1]
 train_happy_sad_model()

0 个答案:

没有答案