English 中文(简体)
• 如何在帐篷中为两层植树层获取狗产品。 掩体使用相应等级,并对嵌入层进行加权?
原标题:How to get the dot product for two embedding layers in tensorflow.keras using the sequential class and set weights for the embedding layers?

I m试图利用<代码>tensorflow.keras建立一个模型,在模型中,我获得两个含有预先界定的重量的嵌入层(在汇编模型时,我能优化)。 我在推论期间的目标是将<条码> 插入_layer_1作为考表,以便获得<条码>的份量。 根据具体指标,因此,我保留<代码>可互用=True。

<代码> 重量_matrix :(288,3569),我谨获得<代码>的印本_layer_1。 (星号为(288,3569))和<编码> 嵌入_layer_2。 (斜体为<代码>(3569,288))。 因此,基本上在<条码>中插入_layer_2,即<条码>的转换。

该网络是:

import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Flatten, Embedding, Dot, Flatten

input_dim=288
output_dim=3569

model_1 = Sequential()
embedding_layer_1 = Embedding(input_dim=input_dim, output_dim=output_dim, name= embedding_layer_1 , dtype= float64 , trainable=True,  input_length=1)
embedding_layer_1.build((None,))
model_1.add(embedding_layer_1)
model_1.layers[0].set_weights([weights_matrix])
model_1.add(Flatten())

model_2 = Sequential()
embedding_layer_2 = Embedding(input_dim=input_dim, output_dim=output_dim, name= embedding_layer_2 , dtype= float64 , trainable=True,  input_length=1)
embedding_layer_2.build((None,))
model_2.add(embedding_layer_2)
model_2.layers[0].set_weights([role_skill_matrix])
model_2.add(Flatten())

dot_product = Dot(axes=-1)([model_1.output, model_2.output])

model = Sequential([model_1, model_2, dot_product])
model.summary()

我有以下错误。 我尝试了上述网络的各种变化,如转播、嵌入_layer_1,并试图获取的印本_layer_1及其移植,以及其他各种改动,但没有任何效果。

I m using tensorflow==2.8.0 and keras==2.8.0.

    model = Sequential([model_1, model_2, dot_product])
  File "/Users/ayalaallon/opt/anaconda3/envs/ml-pipeline/lib/python3.8/site-packages/tensorflow/python/training/tracking/base.py", line 629, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/Users/ayalaallon/opt/anaconda3/envs/ml-pipeline/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/Users/ayalaallon/opt/anaconda3/envs/ml-pipeline/lib/python3.8/site-packages/keras/engine/sequential.py", line 178, in add
    raise TypeError( The added layer must be an instance of class Layer.  
TypeError: The added layer must be an instance of class Layer. Received: layer=KerasTensor(type_spec=TensorSpec(shape=(None, 1), dtype=tf.float32, name=None), name= dot/Squeeze:0 , description="created by layer  dot ") of type <class  keras.engine.keras_tensor.KerasTensor >.

Process finished with exit code 1
最佳回答

I am not sure if you can use layers like the Dot layer in a Sequential Model. The tricky part is that the layers in a Sequential model are stacked.. well, sequentially. How should a layer have more than one input in this type of model, I don t know.
Fortunately, you can just use the functional API to construct the Model, and you started to use the functional syntax at the Dot layer construction yourself.

import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import Flatten, Embedding, Dot, Flatten

input_dim=288
output_dim=3569

in1 = tf.keras.layers.Input(input_dim)
embedding_layer_1 = Embedding(input_dim=input_dim, output_dim=output_dim, name= embedding_layer_1 , 
                              dtype= float64 , trainable=True,  input_length=1)(in1)
flat1 = Flatten()(embedding_layer_1)

in2 = tf.keras.layers.Input(input_dim)
embedding_layer_2 = Embedding(input_dim=input_dim, output_dim=output_dim, name= embedding_layer_2 ,
                              dtype= float64 , trainable=True,  input_length=1)(in2)
flat2 = Flatten()(embedding_layer_2)

dot_product = Dot(axes=-1)([flat1, flat2])

model = Model(inputs=[in1, in2], outputs=[dot_product])
model.summary()

模型显然需要<代码>Input层,作为第一层,没有与相差错。 取消层。 该法律与以下法典一致,没有错误:

import numpy as np
model.compile()

x = np.random.rand(100, 288)
model([x, x])
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...