首页 > 解决方案 > R Keras 连接两个神经网络

问题描述

我需要连接两个神经网络,如下图所示。但是,我收到以下错误消息:

Op 的 float32 类型与参数的 int32 类型不匹配

如何连接两个网络?

源代码:

layer1 <- layer_input(shape = c(MAX), dtype = "int32")
output_tensor <- layer1 %>%
    layer_dense(units = 32, activation = "relu") %>%
    layer_dense(units = 32, activation = "relu")

框架示例

标签: rneural-networkkeraslstm

解决方案


你可以这样做:

library(keras)

max_words <- 20
nb_words <- 1000

text_one_hot <- layer_input(nb_words)
text_as_int <- layer_input(max_words)

vec_1 <- text_one_hot %>%
  layer_dense(100)

vec_2 <- layer_embedding(
  input_dim = nb_words, output_dim = 128, 
  input_length = max_words
  ) %>%
  layer_lstm(128)

out <- layer_concatenate(list(vect_1, vec_2))

model <- keras_model(list(input_1, input_2))

这个链接有一个类似的例子。


推荐阅读