我正在使用class Net(tf.keras.layers.Layer):
def __init__(self):
super(Net, self).__init__()
self.l1 = tf.keras.layers.Dense(5)
def call(self, x):
return self.l1(x)
net=Net()
opt = tf.keras.optimizers.Adam(0.1)
ckpt = tf.train.Checkpoint(step=tf.Variable(1), optimizer=opt, net=net)
manager = tf.train.CheckpointManager(ckpt, '/resources/checkpoints_test2', max_to_keep=3)
dt = np.arange(10,dtype=np.float32)[:,None]
with tf.Session() as sess_inf:
ckpt.restore(manager.latest_checkpoint)
nn = ckpt.net
oo = ckpt.optimizer
op = nn(dt)
print(net.get_weights())
print(opt.get_weights())
构建我的自定义模型。我可以成功保存检查点,但是无法还原它。以下是我的还原脚本。
net
我得到opt
的随机权重,op = nn(dt)
的空列表。而且,如果我评论net
行,则tf.keras.layers.Layer
权重也为空。我知道在op = nn(dt)
类型的模型中,还原必须在变量的创建和首次使用(mentioned here)之间进行。因此,这就是为什么我添加了这一行let inputText = `“It Will Feed my Revenge!” To bait fish withal: if it will feed nothing else, it will feed my revenge. He hath disgraced me, and hindered me half a million; laughed at my losses, mocked at my gains, scorned my nation, thwarted my bargains, cooled my friends, heated mine enemies; and what's his reason?`;
function getRandomChar() {
const characters = 'abcdefghijklmnopqrstuvwxyz';
return characters.charAt(Math.floor(Math.random() * characters.length));
}
function getReplacement(char) {
if (/^[^a-z]+$/i.test(char)) {
return char;
}
let replacement = getRandomChar();
if (char.toUpperCase() === char) {
replacement = replacement.toUpperCase();
}
return replacement;
}
function obfuscate(text, start = 0, end) {
end = end || text.length;
const obfuscatedSection = Array.prototype.map.call(text.substring(start,end), getReplacement).join("");
return text.substring(0, start) + obfuscatedSection + text.substring(end);
}
console.log("Original text:", inputText);
console.log("\nObfuscated text:", obfuscate(inputText, 15, 200));
。但是它应该已经恢复了模型的权重,但是我只是获得了随机权重。我在做什么错了?