Exception has occurred: AttributeError ‘InceptionOutputs‘ object has no attribute ‘log_softmax‘

在使用InceptionV3进行训练时遇到AttributeError:'InceptionOutputs'对象没有属性log_softmax。原因是辅助分支aux_logits在评估状态时不使用,导致问题。修正方法是确保仅使用logits输出。

在使用 inception_v3 训练时报的错,当时使用的是如下代码:

. . .
optimizer.zero_grad()
outs_1 = model(imgs_1)
loss = criterion(outs_1,labels)
loss.backward()
optimizer.step()
. . .

就这样报错了,因为平时都是这样写的,没太注意。通过调试观察网络输出outs_1,发现有2种值,outs_1结果如下:

InceptionOutputs(logits=tensor([[-22.2189, -25.1008, -19.7691,   1.4552, -25.5531],
        [-28.8628
``` import tensorflow as tf import tensorflow_hub as hub import tensorflow_text as text # 需要安装 import kagglehub import matplotlib.pyplot as plt import os from sklearn.model_selection import train_test_split # 示例:使用IMDB影评数据集 dataset, info = tf.load('imdb_reviews', with_info=True, as_supervised=True) train_data, test_data = dataset['train'], dataset['test'] # 转换为numpy格式 texts = [ex[0].numpy().decode('utf-8') for ex in train_data] labels = [ex[1].numpy() for ex in train_data] # 使用预训练的小型BERT(如bert_en_uncased_L-4_H-512_A-8) # 下载模型 bert_preprocess = hub.KerasLayer(kagglehub.model_download("tensorflow/bert/tensorFlow2/en-uncased-preprocess")) bert_encoder = hub.KerasLayer(kagglehub.model_download("tensorflow/bert/tensorFlow2/bert-en-uncased-l-4-h-512-a-8")) # 构建分类模型 inputs = tf.keras.layers.Input(shape=(), dtype=tf.string) preprocessed = bert_preprocess(inputs) outputs = bert_encoder(preprocessed) pooled_output = outputs['pooled_output'] x = tf.keras.layers.Dense(64, activation='relu')(pooled_output) predictions = tf.keras.layers.Dense(3, activation='softmax')(x) # 假设3分类 model = tf.keras.Model(inputs=inputs, outputs=predictions) lowadam = tf.keras.optimizers.Adam(learning_rate=3e-5) # 更小的学习率 model.compile(optimizer=lowadam, loss='sparse_categorical_crossentropy', metrics=['accuracy']) # 增加验证集和早停机制 val_texts = ["Great product", "Poor quality", "Average experience"] val_labels = [0, 1, 2] train_texts, val_texts, train_labels, val_labels = train_test_split(texts, labels, test_size=0.2) # 调整训练参数 history = model.fit( texts, labels, validation_data=(val_texts, val_labels), epochs=500, batch_size=32, callbacks=[tf.keras.callbacks.EarlyStopping(patience=3)] ) file = open('./weights.txt', 'w') # 参数提取 for v in model.trainable_variables: file.write(str(v.name) + '\n') file.write(str(v.shape) + '\n') file.write(str(v.numpy()) + '\n') file.close() loss = history.history['loss'] val_loss = history.history['val_loss'] plt.plot(loss, label='Training Loss') plt.plot(val_loss, label='Validation Loss') plt.title('Training and Validation Loss') plt.legend() plt.show() model.save('transform_model.keras')```Exception has occurred: AttributeError module 'tensorflow' has no attribute 'load' File "D:\source\test4\BERTbig.py", line 13, in <module> dataset, info = tf.load('imdb_reviews', with_info=True, as_supervised=True) AttributeError: module 'tensorflow' has no attribute 'load'
03-15
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值