我有一个包含2列的数据集 - 每列包含一组文档.我必须将Col A中的文档与Col B中提供的文档相匹配.这是一个受监督的分类问题.所以我的训练数据包含一个标签栏,表明文件是否匹配.
为了解决这个问题,我创建了一组功能,例如f1-f25(通过比较2个文档),然后在这些功能上训练了二元分类器.这种方法运行得相当好,但现在我想评估这个问题的深度学习模型(特别是LSTM模型).
我keras在Python中使用该库.在浏览了keras文档和在线提供的其他教程后,我成功完成了以下操作:
from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model
# Each document contains a series of 200 words
# The necessary text pre-processing steps have been completed to transform
each doc to a fixed length seq
main_input1 = Input(shape=(200,), dtype='int32', name='main_input1')
main_input2 = Input(shape=(200,), dtype='int32', name='main_input2')
# Next I add a word embedding layer (embed_matrix is separately created
for each word in my vocabulary by reading from a pre-trained …Run Code Online (Sandbox Code Playgroud) 我想知道如何在Keras中实现具有批量标准化(BN)的biLSTM.我知道BN层应该在线性和非线性之间,即激活.使用CNN或Dense图层很容易实现.但是,如何用biLSTM做到这一点?
提前致谢.
我想定义lambda层以将特征与交叉产品组合,然后合并这些模型,就像图.,我该怎么办?

测试model_1,获得128个密度形式密集,使用pywt获取两个64维特征(cA,cD),然后返回cA*cD //当然我想要组合两个模型,但首先尝试model_1.
from keras.models import Sequential,Model
from keras.layers import Input,Convolution2D,MaxPooling2D
from keras.layers.core import Dense,Dropout,Activation,Flatten,Lambda
import pywt
def myFunc(x):
(cA, cD) = pywt.dwt(x, 'db1')
# x=x*x
return cA*cD
batch_size=32
nb_classes=3
nb_epoch=20
img_rows,img_cols=200,200
img_channels=1
nb_filters=32
nb_pool=2
nb_conv=3
inputs=Input(shape=(1,img_rows,img_cols))
x=Convolution2D(nb_filters,nb_conv,nb_conv,border_mode='valid',
input_shape=(1,img_rows,img_cols),activation='relu')(inputs)
x=Convolution2D(nb_filters,nb_conv,nb_conv,activation='relu')(x)
x=MaxPooling2D(pool_size=(nb_pool,nb_pool))(x)
x=Dropout(0.25)(x)
x=Flatten()(x)
y=Dense(128,activation='relu')(x)
cross=Lambda(myFunc,output_shape=(64,))(y)
predictions=Dense(nb_classes,activation='softmax')(cross)
model = Model(input=inputs, output=predictions)
model.compile(loss='categorical_crossentropy',optimizer='adadelta',metrics=['accuracy'])
model.fit(X_train,Y_train,batch_size=batch_size,nb_epoch=nb_epoch,
verbose=1,validation_data=(X_test,Y_test))
Run Code Online (Sandbox Code Playgroud)
对不起,我可以问一个关于张量的问题吗?
import tensorflow as tf
W1 = tf.Variable(np.array([[1,2],[3,4]]))
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
array = W1.eval(sess)
print (array)
Run Code Online (Sandbox Code Playgroud)
那就对了!然而,
from keras …Run Code Online (Sandbox Code Playgroud) 我有一个我在Keras建立的顺序模型.我试图弄清楚如何改变输入的形状.在以下示例中
model = Sequential()
model.add(Dense(32, input_shape=(500,)))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
Run Code Online (Sandbox Code Playgroud)
假设我想构建一个具有不同输入形状的新模型,概念性应该如下所示:
model1 = model
model1.layers[0] = Dense(32, input_shape=(250,))
Run Code Online (Sandbox Code Playgroud)
有没有办法修改模型输入形状?
我希望有人可以解释Keras中的输入层和Tensorflow中的占位符之间的差异(如果有的话)?
我调查的越多,两者看起来越相似,但到目前为止,我不相信100%.
以下是我观察到的支持输入图层和tf占位符相同的说法:
1)从keras.Input()返回的张量可以像tf.Session的run方法的feed_dict中的占位符一样使用.下面是使用Keras的简单示例的一部分,它添加了两个张量(a和b)并将结果与第三个张量(c)连接起来:
model = create_graph()
con_cat = model.output[0]
ab_add = model.output[1]
# These values are used equivalently to tf.Placeholder() below
mdl_in_a = model.input[0]
mdl_in_b = model.input[1]
mdl_in_c = model.input[2]
sess = k.backend.get_session()
a_in = rand_array() # 2x2 numpy arrays
b_in = rand_array()
c_in = rand_array()
a_in = np.reshape( a_in, (1,2,2))
b_in = np.reshape( b_in, (1,2,2))
c_in = np.reshape( c_in, (1,2,2))
val_cat, val_add = sess.run([con_cat, ab_add],
feed_dict={ mdl_in_a: a_in, mdl_in_b: b_in, mdl_in_c: c_in})
Run Code Online (Sandbox Code Playgroud)
2)来自Tensorflow Contrib的关于Keras 输入层的文档在其参数描述中提到了占位符:
"sparse:一个布尔值,指定要创建的占位符是否稀疏"
以下是我观察到的支持输入图层和tf占位符不同的说法: …
根据这个下面复制权从一个模式到另一个:
target_model.set_weights(model.get_weights())
Run Code Online (Sandbox Code Playgroud)
复制特定层的权重怎么样,这行得通吗?
model_1.layers[0].set_weights(source_model.layers[0].get_weights())
model_2.layers[0].set_weights(source_model.layers[0].get_weights())
Run Code Online (Sandbox Code Playgroud)
如果我训练model_1,model_2他们会有单独的重量吗?该文档没有说明这是否get_weights进行了深层复制。如果这不起作用,如何实现?
我是keras和tensorflow的新手。当我使用tensorflow编写程序时,我必须建立一个会话来运行图形。但是,当我使用keras时,尽管后端显然是tensorflow,但我在keras代码中看不到session。似乎所有事情都在model.compile和model.fit之后完成。
那么,Keras如何工作?张量流会话在哪里?而不是会话,我可以对keras使用渴望执行吗?
预先感谢,对不起我的英语
我试图运行一些代码来创建LSTM模型,但出现错误:
AttributeError: module 'tensorflow' has no attribute 'get_default_graph'
我的代码如下:
from keras.models import Sequential
model = Sequential()
model.add(Dense(32, input_dim=784))
model.add(Activation('relu'))
model.add(LSTM(17))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
Run Code Online (Sandbox Code Playgroud)
我发现其他人也有类似的问题,他们更新了tensorflow并成功了。但是我的是最新的,但仍然无法正常工作。我是新来的使用keras和机器学习的人,因此如果这很愚蠢,我深表歉意!
对于我拥有的每个输入,我都有一个关联的 49x2 矩阵。这是一对输入输出对的样子
input :
[Car1, Car2, Car3 ..., Car118]
output :
[[Label1 Label2]
[Label1 Label2]
...
[Label1 Label2]]
Run Code Online (Sandbox Code Playgroud)
其中 Label1 和 Label2 都是 LabelEncode,它们分别有 1200 和 1300 个不同的类。
只是为了确保这就是我们所说的多输出多类问题?
我试图展平输出,但我担心模型无法理解所有类似的 Label 共享相同的类。
是否有一个 Keras 层可以处理输出这种奇特的数组形状?
我试图将scikit-learn的PolynomialFeatures实现为tensorflow和Keras中的前馈神经网络中的一个层.为简单起见,我将举例说明使用NumPy数组.如果批次具有三个样本并且某个层的激活等于(3,2)形矩阵
>>> X = np.arange(0, 6).reshape(2, 3)
>>> X
array([[0, 1],
[2, 3],
[4, 5]])
Run Code Online (Sandbox Code Playgroud)
然后我希望下一层中的激活等于2次多项式特征扩展X:
>>> from sklearn.preprocessing import PolynomialFeatures
>>> PolynomialFeatures(degree=2).fit_transform(X)
array([[ 1., 0., 1., 0., 0., 1.],
[ 1., 2., 3., 4., 6., 9.],
[ 1., 4., 5., 16., 20., 25.]])
Run Code Online (Sandbox Code Playgroud)
也就是说,如果层i的激活是X(形状(batch_size, num_features))矩阵,那么对于参数选择,degree=2我希望层i + 1的激活是一个串联
batch_size许多1.人的专栏,X 本身,X:X[:, 0] * X[:, 0],X[:, 0] * …