python - “模型”对象没有属性“损失函数”
问题描述
我已经完成了 NLP 的 udacity 纳米学位。我为该项目使用了 udacity 平台,但我现在正在尝试使用我自己的本地机器来训练模型等。
我终于解决了我的 GPU/tensorflow 问题(我认为),但我遇到了一些我认为与 udacity 使用的 tensorflow 版本有关的问题。
我目前正在使用 TensorFlow 2.2
具体来说,我从项目用来列出损失函数的验证步骤中得到一个错误。
def _test_model(model, input_shape, output_sequence_length, french_vocab_size):
if isinstance(model, Sequential):
model = model.model
print(model.loss_functions)
当调用它时,我得到“'Model' object has no attribute 'loss_functions'”错误。
该模型是使用以下代码构建的。
def simple_model(input_shape, output_sequence_length, english_vocab_size, french_vocab_size):
"""
Build and train a basic RNN on x and y
:param input_shape: Tuple of input shape
:param output_sequence_length: Length of output sequence
:param english_vocab_size: Number of unique English words in the dataset
:param french_vocab_size: Number of unique French words in the dataset
:return: Keras model built, but not trained
"""
# TODO: Build the layers
learning_rate = 0.01
#Config Model
inputs = Input(shape=input_shape[1:])
hidden_layer = GRU(output_sequence_length, return_sequences=True)(inputs)
outputs = TimeDistributed(Dense(french_vocab_size, activation='softmax'))(hidden_layer)
#Create Model from parameters defined above
model = keras.Model(inputs=inputs, outputs=outputs)
#loss_function = 'sparse_categorical_crossentropy'
loss_fn = keras.losses.SparseCategoricalCrossentropy()
model.compile(loss=loss_fn,optimizer=Adam(learning_rate),metrics=['accuracy'])
我一直在使用以下库
import tensorflow as tf
from tensorflow.keras.losses import sparse_categorical_crossentropy
from tensorflow.keras.optimizers import Adam
from tensorflow import keras
import collections
import helper
import numpy as np
import project_tests as tests
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras.layers import GRU, Input, Dense, TimeDistributed, Activation, RepeatVector, Bidirectional, Dropout
from tensorflow.keras.layers.embeddings import Embedding
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import sparse_categorical_crossentropy
我可以注释掉这个检查损失函数,但我真的很想了解发生了什么。
谢谢
解决方案
我认为 API 在 Tensorflow 2 中发生了变化,做了以下工作:
model.compiled_loss._get_loss_object(model.compiled_loss._losses).fn
推荐阅读
- c# - 地图的 C# 图像放置图钉并将鼠标悬停在信息上?
- html - 有没有办法在 Vue 参数中创建超链接
- python - 获取圆形openCV内的非黑色像素
- javascript - Leaflet JS iconUrl & Laravel
- javascript - 如何在 truffle 迁移脚本中使用 truffle deployer 嵌套 promise
- javascript - 根据悬停的图像播放特定音频(javascript)
- javascript - React Native react-native-paper 按钮具有更大的高度
- c++ - 在 cpp 中重新定义 new 和 delete 运算符以跟踪内存分配:无法跟踪整个内存块的删除大小
- php - Laravel:如何使用 group By 和 sum 并选择多字段
- angular - Angular:如何记录内存使用情况?