首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取"ValueError:‘输出’必须在循环之前定义。“拟合误差模型

获取"ValueError:‘输出’必须在循环之前定义。“拟合误差模型
EN

Stack Overflow用户
提问于 2021-07-08 04:08:11
回答 1查看 1.3K关注 0票数 2

我制作了一个CNN模型,用来训练b/w图像,以便在TPU上训练100*100。添加了基本的回调,但是在运行它之后,它给出的输出必须定义错误。

代码语言:javascript
复制
def create_model():
    model = Sequential()
    model.add(InputLayer(input_shape=(100, 100, 1)))

# Building the neural network
    model = Sequential()
    model.add(InputLayer(input_shape=(100, 100, 1)))
    model.add(Conv2D(8, (3, 3), activation='relu', padding='same', strides=2))
    model.add(Conv2D(8, (3, 3), activation='relu', padding='same'))
    model.add(Conv2D(16, (3, 3), activation='relu', padding='same'))
    model.add(Conv2D(16, (3, 3), activation='relu', padding='same', strides=2))
    model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
    model.add(Conv2D(32, (3, 3), activation='relu', padding='same', strides=2))
    model.add(UpSampling2D((2, 2)))
    model.add(Conv2D(32, (3, 3), activation='relu', padding='same'))
    model.add(UpSampling2D((2, 2)))
    model.add(Conv2D(16, (3, 3), activation='relu', padding='same'))
    model.add(UpSampling2D((2, 2)))
    model.add(Conv2D(2, (3, 3), activation='tanh', padding='same'))
    model.compile(
    optimizer='adam',
    loss = 'binary_crossentropy',
    metrics=['accuracy'], experimental_steps_per_execution = 20)
    return model
    
with tpu_strategy.scope(): # creating the model in the TPUStrategy scope means we will train the model on the TPU
  model = create_model()
model.summary()

history = model.fit(X_train,y_train,validation_data = (X_test,y_test),steps_per_epoch=20, epochs=100, callbacks=[lr_callback])

final_accuracy = history.history["val_accuracy"][-5:]
print("FINAL ACCURACY MEAN-5: ", np.mean(final_accuracy))

但我发现了这个错误

代码语言:javascript
复制
ValueError: in user code:

    /opt/conda/lib/python3.7/site-packages/tensorflow/python/keras/engine/training.py:811 train_function  *
        for _ in math_ops.range(self._steps_per_execution):
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/operators/control_flow.py:414 for_stmt
        symbol_names, opts)
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/operators/control_flow.py:629 _tf_range_for_stmt
        opts)
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/operators/control_flow.py:1059 _tf_while_stmt
        body, get_state, set_state, init_vars, nulls, symbol_names)
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/operators/control_flow.py:1032 _try_handling_undefineds
        _verify_loop_init_vars(init_vars, symbol_names, first_iter_vars)
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/autograph/operators/control_flow.py:193 _verify_loop_init_vars
        raise ValueError(error_msg)

    ValueError: 'outputs' must be defined before the loop.
EN

回答 1

Stack Overflow用户

发布于 2022-04-22 11:10:09

删除"experimental_steps_per_execution“以查看真正的错误消息。当您使用experimental_steps_per_execution>1时,会定义一个GPU循环来优化性能,而不是将数据发回给CPU,但是这个循环隐藏了错误。

首先修复错误,然后使用experimental_steps_per_execution>1

祝你好运

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68295502

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档