小编Ros*_*Liu的帖子

LSTM 如何处理变长序列

我在用 Python进行深度深度学习的第 7 章第 1 节中找到了一段代码,如下所示?

from keras.models import Model
from keras import layers
from keras import Input

text_vocabulary_size = 10000
question_vocabulary_size = 10000
answer_vocabulary_size = 500

# Our text input is a variable-length sequence of integers.
# Note that we can optionally name our inputs!
text_input = Input(shape=(None,), dtype='int32', name='text')

# Which we embed into a sequence of vectors of size 64
embedded_text = layers.Embedding(64, text_vocabulary_size)(text_input)

# Which we encoded in a single vector via …
Run Code Online (Sandbox Code Playgroud)

python machine-learning deep-learning lstm keras

8
推荐指数
1
解决办法
1万
查看次数

如何处理keras:错误:维度0的切片索引0越界

我使用 keras(tensorflow backend) 来构建我的 lstm 网络,这是我的代码:

from keras.models import Sequential,Model
from keras.layers import LSTM,Conv1D,Dense,MaxPooling1D,GlobalMaxPooling1D,Input,Concatenate
from keras.optimizers import Adam

x_input = Input(shape=(None,x_train.shape[-1]),name='input')
x_mid = Conv1D(32,4, activation='relu')(x_input)
x_mid = MaxPooling1D(3)(x_mid)
x_mid = Conv1D(32,4,activation = 'relu')(x_mid)
x_mid = LSTM(32,dropout=0.1, recurrent_dropout=0.2,activation='relu')(x_mid)
x_mid = Dense(1,activation='sigmoid')(x_mid)
other_input = Input(shape=(x_blend_train.shape[-1],),name='clfs_input')
merge_x = concatenate(inputs= [x_mid,other_input],axis = -1)
output = Dense(32,activation='relu')(merge_x)
output = Dense(1,activation='sigmoid')(output)
model = Model(inputs=[x_input,other_input],outputs=output)
model.compile(optimizer='adam',loss=['binary_crossentropy'],metrics=['acc'])
model.summary()
Run Code Online (Sandbox Code Playgroud)

这就是我的网络的样子

Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input (InputLayer)              (None, None, 49)     0                                            
__________________________________________________________________________________________________
conv1d_56 (Conv1D)              (None, …
Run Code Online (Sandbox Code Playgroud)

deep-learning keras tensorflow

4
推荐指数
1
解决办法
9553
查看次数

如何使用生成器初始化numpy数组?

代码如下:

def computerCost(x,y,theta):
    m = len(y)
    J = np.sum((np.dot(x,theta) - y)**2) /(2*m)
    return J

m = 100
x = np.linspace(-5,10,m)
y = np.linspace(1,100,m)
x, y = x.reshape(m,1), y.reshape(m,1)
theta_0 = np.linspace(-10,10,100)
theta_1 = np.linspace(-1,4,100)
X,Y = np.meshgrid(theta_0,theta_1)

###### Here I want to initialize a numpy array with generator.
J_vals = np.array(computerCost(x,y,np.array([a,b])) for a,b in zip(np.ravel(X), np.ravel(Y)) )

print('out:',J_vals)
Run Code Online (Sandbox Code Playgroud)

在Python 3.5中运行此代码给出:

out:<generator object <genexpr> at 0x0000028ACF28B258>
Run Code Online (Sandbox Code Playgroud)

控制台打印出J_vals是一个生成器.有没有办法将发电机改成np.ndarrray

python numpy

3
推荐指数
1
解决办法
565
查看次数