我已经完成了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)当它被调用时,我得到“‘模型’对象没有属性'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我可以注释掉损失函数的检查,但我真的很想了解发生了什么。
谢谢
发布于 2020-12-28 02:12:48
我认为Tensorflow 2中的API改变了,做了以下工作:
model.compiled_loss._get_loss_object(model.compiled_loss._losses).fnhttps://stackoverflow.com/questions/65468878
复制相似问题