我一直在关注 Towards Data Science 关于 word2vec 和 skip-gram 模型的教程,但我偶然发现了一个我无法解决的问题,尽管搜索了几个小时并尝试了很多不成功的解决方案。
由于使用了 keras.layers 中的 Merge 层,它向您展示了如何构建 skip-gram 模型架构的步骤似乎已被弃用。
我似乎对此进行了很多讨论,大多数答案是您现在需要使用 Keras 的功能 API 来合并层。但问题是,我是 Keras 的初学者,不知道如何将我的代码从 Sequential 转换为 Functional,这是作者使用的代码(我复制了):
from keras.layers import Merge
from keras.layers.core import Dense, Reshape
from keras.layers.embeddings import Embedding
from keras.models import Sequential
# build skip-gram architecture
word_model = Sequential()
word_model.add(Embedding(vocab_size, embed_size,
embeddings_initializer="glorot_uniform",
input_length=1))
word_model.add(Reshape((embed_size, )))
context_model = Sequential()
context_model.add(Embedding(vocab_size, embed_size,
embeddings_initializer="glorot_uniform",
input_length=1))
context_model.add(Reshape((embed_size,)))
model = Sequential()
model.add(Merge([word_model, context_model], mode="dot"))
model.add(Dense(1, kernel_initializer="glorot_uniform", activation="sigmoid"))
model.compile(loss="mean_squared_error", optimizer="rmsprop")
# view model summary …Run Code Online (Sandbox Code Playgroud) 我读了几个像我这样的问题,但似乎没有人遇到与我相同的问题。
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
facebook_users = [10, 14, 19, 17, 17, 13, 10]
twitter_users = [10, 18, 22, 17, 15, 11, 7]
linkedin_users = [4, 10, 20, 18, 20, 17, 11]
group = ['13 - 17', '18 - 24', '25 - 34', '35 - 44', '45 - 54', '55 - 65', '65+']
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=["#3B5998", "c", "grey"])
def bar_group(classes, values, width=0.8):
plt.xlabel('Age groups (in years)', weight='semibold')
plt.ylabel('Percentage of users (%)', weight='semibold') …Run Code Online (Sandbox Code Playgroud)