属性错误:“嵌入”对象没有属性“嵌入” - TensorFlow 和 Keras

问题描述 投票:0回答:3

好吧,我有一个 keras 模型,我完全运行了它,然后用这行代码保存了权重:

model.save_weights("rho_beta_true_tf", save_format="tf")

然后在另一个文件中,我仅构建模型,然后使用此行从上面运行的模型加载权重:

model_build.load_weights("rho_beta_true_tf")

当我去调用某些属性时,所有内容都会正确显示,除非我尝试运行此行:

model_build.stimuli.embeddings 

model_build.stimuli.embeddings.numpy()[0]

我收到属性错误:

AttributeError: 'Embedding' object has no attribute 'embeddings'

这条线应该返回一个张量,如果我到目前为止调用任何其他属性它会起作用,所以我不确定它是否只是找不到张量或者问题是否是其他问题。有人可以帮我弄清楚如何解决这个属性错误吗?

python tensorflow keras keras-layer
3个回答
0
投票

尝试使用

.get_weights()
:

model_build.stimuli.get_weights()

0
投票

事实证明,因为我已将权重保存为 tf 格式,所以我必须遵循张量流文档中的此步骤:

对于继承自 tf.keras.Model 的用户定义类,必须将 Layer 实例分配给对象属性,通常在构造函数中。

那么接下来的路线

build_model.stimuli.embedding(put the directory path to your custom embedding layer here)

成功了!


0
投票
import torch
import torch.nn as nn
import torch.nn.functional as F

# Definition of a basic neural network class
class SimpleBrokenModel(nn.Module):
    def __init__(self, config=MASTER_CONFIG):
        super(SimpleBrokenModel, self).__init__()  # Inherit from nn.Module
        # Rest of the code ...

    # Forward pass function for the base model
    def forward(self, idx, targets=None):
        # Embedding layer converts character indices to vectors
        x = self.embedding(idx)

        # Linear layers for modeling relationships between features
        a = self.linear(x)

        # Apply softmax activation to obtain probability distribution
        logits = F.softmax(a, dim=-1)

        # If targets are provided, calculate and return the cross-entropy loss
        if targets is not None:
            # Reshape logits and targets for cross-entropy calculation
            loss = F.cross_entropy(logits.view(-1, self.config['vocab_size']), targets.view(-1))
            return logits, loss
        # If targets are not provided, return the logits
        else:
            return logits

# Update MASTER_CONFIG with the dimension of linear layers (128)
MASTER_CONFIG.update({
    'd_model': 128,
})

# Instantiate the SimpleBrokenModel using the updated MASTER_CONFIG
model = SimpleBrokenModel(MASTER_CONFIG)

# Print the total number of parameters in the model
print("Total number of parameters in the Simple Neural Network Model:", sum([m.numel() for m in model.parameters()]))
for the above code im getting error please help me...

# Obtain batches for training using the specified batch size and context window
xs, ys = get_batches(dataset, 'train', MASTER_CONFIG['batch_size'], MASTER_CONFIG['context_window'])

# Calculate logits and loss using the model
logits, loss = model(xs, ys)`enter code here`
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.