首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Tensorflow2.0卷积时报错?

Tensorflow2.0卷积时报错?

提问于 2019-12-06 09:39:22
回答 1关注 0查看 3.6K

from __future__ import absolute_import, division, print_function, unicode_literals

import os

import os.path as osp

import tensorflow as tf

from tensorflow import keras

from tensorflow.keras import datasets, layers, models

#import cv2

import numpy as np

import matplotlib.pyplot as plt

# =====================================================================

# 不加这几句,则CONV 报错

physical_devices = tf.config.experimental.list_physical_devices('GPU')

assert len(physical_devices) > 0, "Not enough GPU hardware devices available"

tf.config.experimental.set_memory_growth(physical_devices[0], True)

(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()

train_images = train_images.reshape((60000, 28, 28, 1))

test_images = test_images.reshape((10000, 28, 28, 1))

# 特征缩放[0, 1]区间

train_images, test_images = train_images / 255.0, test_images / 255.0

model = models.Sequential()

model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))

model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Conv2D(64, (3, 3), activation='relu'))

model.add(layers.MaxPooling2D((2, 2)))

model.add(layers.Conv2D(64, (3, 3), activation='relu'))

model.add(layers.Flatten())

model.add(layers.Dense(64, activation='relu'))

model.add(layers.Dense(10, activation='softmax'))

model.summary() # 显示模型的架构

model.compile(optimizer='adam',

loss='sparse_categorical_crossentropy',

metrics=['accuracy'])

model.fit(train_images, train_labels, epochs=5,batch_size=64)

运行后报错:

RuntimeError: Physical devices cannot be modified after being initialized

小白一枚,该怎么办啊,调了好多次,网上都没有该类描述,心态要蹦了

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

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