运行Keras例子程序:imdb_cnn.py

下载例子程序:https://github.com/fchollet/keras

函数解析

1)keras.layers.embeddings.Embedding(input_dim, output_dim, init='uniform', input_length=None, W_regularizer=None, activity_regularizer=None, W_constraint=None, mask_zero=False, weights=None, dropout=0.0)

Example

# -*- coding: utf-8 -*-
"""
Created on Mon Jun 06 15:30:07 2016

@author: Michelle
"""
from __future__ import print_function
import numpy as np
from keras.models import Sequential
from keras.layers import Embedding

model = Sequential()
#该模型的输入为一个整数矩阵(batch, input_length),
#输出为三维矩阵(batch, input_length, 64),即每行的输入生成一个矩阵(input_length, 64)
model.add(Embedding(1000, 64, input_length=10))
  # the model will take as input an integer matrix of size (batch, input_length).
  # the largest integer (i.e. word index) in the input should be no larger than 999 (vocabulary size).
  # now model.output_shape == (None, 10, 64), where None is the batch dimension.

#输入:1000以内,32*10的随机二维整数矩阵
input_array = np.random.randint(1000, size=(32, 10))

model.compile('rmsprop', 'mse')
#输出:三维矩阵
output_array = model.predict(input_array)
assert output_array.shape == (32, 10, 64)

运行程序时出现的错误:

1)EOFError

原因:重复下载文件没有中断

解决:到 C:\Users\Michelle\.keras\datasets下删除*.pkl文件

你可能感兴趣的:(机器学习)