训练使用tensorflow.keras.Model和keras函数API设计的网络会导致Python崩溃

2024-04-24 22:19:01 发布

您现在位置:Python中文网/ 问答频道 /正文

-Keras版本:2.3.1 -Tensorflow版本:2.2.0 -操作系统:Windows 10 -运行于:CPU处理器Intel(R)Core(TM)i7-8850H -发展于:PyCharm -Python版本:3.7

我尝试过几次培训使用keras函数API设计的网络,但是培训总是导致python崩溃,并显示以下退出消息:

Process finished with exit code -1073740791 (0xC0000409)

没有提供其他堆栈跟踪。下面是我尝试运行的代码的简化版本:

import scipy.io as io
import os
import numpy as np
from tensorflow.keras import layers, losses, Input
from tensorflow.keras.models import Model

directory = 'C:\\Dataset'

def simple_generator(dim1, dim2, dim3, batch_size=5):
    while True:
        samples = np.random.random_sample((batch_size, dim1, dim2, dim3))
        targets = np.random.random_sample((batch_size, 3))
        yield samples, targets

example_shape = (1100, 4096, 2)

train_gen = simple_generator(example_shape[0], example_shape[1], example_shape[2], batch_size=10)
val_gen = simple_generator(example_shape[0], example_shape[1], example_shape[2])
test_gen = simple_generator(example_shape[0], example_shape[1], example_shape[2])

input_tensor = Input(shape=example_shape)
preprocess = layers.Conv2D(32, 3, activation='relu')(input_tensor)
preprocess = layers.Conv2D(32, 3, activation='relu')(preprocess)
preprocess = layers.MaxPool2D(pool_size=(3, 3), strides=3)(preprocess)

"""   Head 1   """
head_1 = layers.Conv2D(32, 3, activation='relu')(preprocess)
head_1 = layers.Conv2D(32, 3, activation='relu')(head_1)
head_1 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_1)
head_1 = layers.Conv2D(32, 3, activation='relu')(head_1)
head_1 = layers.Conv2D(32, 3, activation='relu')(head_1)
head_1 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_1)
head_1 = layers.Conv2D(16, 3, activation='relu')(head_1)
head_1 = layers.Conv2D(16, 3, activation='relu')(head_1)
head_1 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_1)
head_1 = layers.Dense(8, activation='relu')(head_1)

"""   Head 2   """
head_2 = layers.Conv2D(32, 3, activation='relu')(preprocess)
head_2 = layers.Conv2D(32, 3, activation='relu')(head_2)
head_2 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_2)
head_2 = layers.Conv2D(32, 3, activation='relu')(head_2)
head_2 = layers.Conv2D(32, 3, activation='relu')(head_2)
head_2 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_2)
head_2 = layers.Conv2D(16, 3, activation='relu')(head_2)
head_2 = layers.Conv2D(16, 3, activation='relu')(head_2)
head_2 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_2)
head_2 = layers.Dense(8, activation='relu')(head_2)

"""   Head 3   """
head_3 = layers.Conv2D(32, 3, activation='relu')(preprocess)
head_3 = layers.Conv2D(32, 3, activation='relu')(head_3)
head_3 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_3)
head_3 = layers.Conv2D(32, 3, activation='relu')(head_3)
head_3 = layers.Conv2D(32, 3, activation='relu')(head_3)
head_3 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_3)
head_3 = layers.Conv2D(16, 3, activation='relu')(head_3)
head_3 = layers.Conv2D(16, 3, activation='relu')(head_3)
head_3 = layers.MaxPool2D(pool_size=(3, 3), strides=3)(head_3)
head_3 = layers.Dense(8, activation='relu')(head_3)

concat_out = layers.Concatenate(axis=-1)([head_1, head_2, head_3])
concat_out = layers.Flatten()(concat_out)
output_tensor = layers.Dense(3)(concat_out)
model = Model(input_tensor, output_tensor)

model.compile(loss=losses.mean_absolute_error, optimizer='sgd')
model.summary()

history = model.fit(x=train_gen, steps_per_epoch=25, epochs=12, validation_data=val_gen, validation_steps=10, verbose=True)
model.save(directory + '\\divergent_network.h5')
evaluations = model.evaluate_generator(generator=test_gen, steps=20, verbose=True)
print(evaluations)

有人能解释这次事故或提出任何可能的解决方案吗?我在Anaconda环境中运行这段代码,但是我尝试在基本的python虚拟环境中运行,以查看这是否是Anaconda问题,但我没有看到两者之间的区别


Tags: sizemodelexamplelayersgeneratoractivationheadgen