首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“‘Model”对象没有属性“loss_functions”

“‘Model”对象没有属性“loss_functions”
EN

Stack Overflow用户
提问于 2020-12-28 02:07:11
回答 1查看 334关注 0票数 0

我已经完成了NLP的udacity纳米学位。我在项目中使用了udacity平台,但现在我正在尝试使用自己的本地机器来训练模型等。

我终于解决了我的GPU/tensorflow问题(我想),但我遇到了一些问题,我相信这些问题与udacity使用的tensorflow版本有关。

我当前使用的是TensorFlow 2.2

具体地说,我从项目用来列出损失函数的验证步骤中得到一个错误。

代码语言:javascript
复制
def _test_model(model, input_shape, output_sequence_length, french_vocab_size):
    if isinstance(model, Sequential):
        model = model.model
        
    print(model.loss_functions)

当它被调用时,我得到“‘模型’对象没有属性'loss_functions'”错误。

该模型是使用以下代码构建的。

代码语言:javascript
复制
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'])

在此过程中,我使用了下面的库

代码语言:javascript
复制
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

我可以注释掉损失函数的检查,但我真的很想了解发生了什么。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-28 02:12:48

我认为Tensorflow 2中的API改变了,做了以下工作:

代码语言:javascript
复制
model.compiled_loss._get_loss_object(model.compiled_loss._losses).fn
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65468878

复制
相关文章

相似问题

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