我正在尝试使用 tfgo 包在 Go 中实现我的 Keras 神经网络。该模型包括 2 个常规输入和两个 Keras 嵌入层。它看起来像这样:
embedding_layer = Embedding(vocab_size,
100,
weights=[embedding_matrix],
input_length=100,
trainable=False)
sequence_input = Input(shape=(max_length,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
text_lstm = Bidirectional(LSTM(256))(embedded_sequences)
text_lstm = Dropout(0.5)(text_lstm)
text_lstm = Dense(512, activation='relu')(text_lstm )
text_lstm = Dropout(0.5)(text_lstm)
text_lstm = Dense(256, activation='relu')(text_lstm)
text_lstm = Dropout(0.5)(text_lstm)
text_lstm = Dense(128, activation='relu')(text_lstm)
text_lstm = Dropout(0.5)(text_lstm)
title_input = Input(shape=(max_title_length,), dtype='int32')
title_embed = Embedding(vocab_size, embedding_vector_length, input_length=max_title_length)(title_input)
title_lstm = Bidirectional(LSTM(128))(title_embed)
title_lstm = Dropout(0.5)(title_lstm)
title_lstm = Dense(512, activation='relu')(title_lstm )
title_lstm = Dropout(0.5)(title_lstm)
title_lstm = Dense(256, activation='relu')(title_lstm)
title_lstm = Dropout(0.5)(title_lstm)
title_lstm = Dense(128, activation='relu')(title_lstm)
title_lstm = Dropout(0.5)(title_lstm)
merged = concatenate([text_lstm, title_lstm])
merged_d1 = Dense(1024, activation='relu')(merged)
merged_d1 = Dropout(0.5)(merged_d1)
merged_d1 = Dense(512, activation='relu')(merged_d1)
merged_d1 = Dropout(0.5)(merged_d1)
text_class = Dense(num_classes, activation='sigmoid')(merged_d1)
model = Model([sequence_input, title_input], text_class)
我正在尝试在 Go 中加载模型,到目前为止我认为我已经能够像这样包含常规输入层:
s := make([]int32, 100)
s1 := make([]int32, 15)
model := tg.LoadModel("myModel3", []string{"myTag"}, nil)
tensor1, _ := tf.NewTensor(s)
tensor2, _ := tf.NewTensor(s1)
result := model.Exec([]tf.Output{
model.Op("dense_18/Sigmoid", 0),
}, map[tf.Output]*tf.Tensor{
model.Op("input_1", 0): tensor1,
model.Op("input_3", 0): tensor2,
})
但是当我运行代码时,它提醒我实际上还有两个“输入”:
panic: You must feed a value for placeholder tensor 'input_4' with dtype int32 and shape [?,15]
[[Node: input_4 = Placeholder[_output_shapes=[[?,15]], dtype=DT_INT32, shape=[?,15], _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]
我想这些将是“input2”和“input4”,并且它们需要以某种方式使用模型的嵌入进行初始化,但我不知道如何在 Go 的 Tensorflow 中执行此操作(我是Go 的新手)。
我尝试了以下方法:
s := make([]int32, 100)
s1 := make([]int32, 15)
tensor1e, _ := tf.NewTensor([1][100][2]float32{})
tensor2e, _ := tf.NewTensor([1][15][2]float32{})
tensor1, _ := tf.NewTensor(s)
tensor2, _ := tf.NewTensor(s1)
result := model.Exec([]tf.Output{
model.Op("dense_18/Sigmoid", 0),
}, map[tf.Output]*tf.Tensor{
model.Op("input_3", 0): tensor1,
model.Op("embedding_2/embeddings", 0): tensor2e,
model.Op("embedding_1/embeddings", 0): tensor1e,
model.Op("input_4", 0): tensor2,
})
But this
产生了以下错误:
2018-08-17 19:50:00.543771: W tensorflow/core/framework
/op_kernel.cc:1275] OP_REQUIRES failed at transpose_op.cc:157 : Invalid argument: transpose expects a vector of size 2. But input(1) is a vector of size 3
2018-08-17 19:50:00.543792: W tensorflow/core/framework/op_kernel.cc:1275] OP_REQUIRES failed at reduction_ops_common.h:155 : Invalid argument: Invalid reduction dimension (2 for input with 2 dimension(s)
panic: Invalid reduction dimension (2 for input with 2 dimension(s)
[[Node: bidirectional_4/Sum = Sum[T=DT_FLOAT, Tidx=DT_INT32, _output_shapes=[[?]], keep_dims=false, _device="/job:localhost/replica:0/task:0/device:CPU:0"](bidirectional_4/zeros_like, bidirectional_3/Sum/reduction_indices)]]
任何人都可以指出我如何完成此操作的正确方向吗?任何帮助将非常感激!
最佳答案
因此,事实证明我不需要为嵌入层指定输入。我实际上是在错误地构建输入。它应该是这样的:
tensor1, _ := tf.NewTensor([][]int32{tokes_text})
tensor2, _ := tf.NewTensor([][]int32{tokes_title})
result := model.Exec([]tf.Output{
model.Op("dense_18/Sigmoid", 0),
}, map[tf.Output]*tf.Tensor{
model.Op("input_3", 0): tensor1,
model.Op("input_4", 0): tensor2,
})
关于python - 在 Golang 的 Tensorflow 中打开带有嵌入层的 Keras 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51904143/
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:
对于Rails模型,是否可以/建议让一个类的成员不持久保存到数据库中?我想将用户最后选择的类型存储在session变量中。由于我无法从我的模型中设置session变量,我想将值存储在一个“虚拟”类成员中,该成员只是将值传递回Controller。你能有这样的类(class)成员吗? 最佳答案 将非持久属性添加到Rails模型就像任何其他Ruby类一样:classUser扩展解释:在Ruby中,所有实例变量都是私有(private)的,不需要在赋值前定义。attr_accessor创建一个setter和getter方法:classUs
我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案