Movie title, which is a sequence of words represented by an integer word index sequence, will be feed into a `sequence_conv_pool` layer, which will apply convolution and pooling on time dimension. Because pooling is done on time dimension, the output will be a fixed-length vector regardless the length of the input sequence.
Movie title, which is a sequence of words represented by an integer word index sequence, will be fed into a `sequence_conv_pool` layer, which will apply convolution and pooling on time dimension. Because pooling is done on time dimension, the output will be a fixed-length vector regardless the length of the input sequence.
Finally, we can define a `inference_program` that use cosine similarity to calculate the similarity between user characteristics and movie features.
Finally, we can define a `inference_program` that uses cosine similarity to calculate the similarity between user characteristics and movie features.
```python
```python
definference_program():
definference_program():
...
@@ -331,7 +331,8 @@ def inference_program():
...
@@ -331,7 +331,8 @@ def inference_program():
returnscale_infer
returnscale_infer
```
```
Then we define a `training_program` that uses the result from `inference_program` to compute the cost with label data
Then we define a `training_program` that uses the result from `inference_program` to compute the cost with label data.
Also define `optimizer_func` to specify the optimizer.
```python
```python
deftrain_program():
deftrain_program():
...
@@ -343,6 +344,10 @@ def train_program():
...
@@ -343,6 +344,10 @@ def train_program():
avg_cost=layers.mean(square_cost)
avg_cost=layers.mean(square_cost)
return[avg_cost,scale_infer]
return[avg_cost,scale_infer]
defoptimizer_func():
returnfluid.optimizer.SGD(learning_rate=0.2)
```
```
## Model Training
## Model Training
...
@@ -358,8 +363,8 @@ place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
...
@@ -358,8 +363,8 @@ place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
### Datafeeder Configuration
### Datafeeder Configuration
Next we define data feeders for test and train. The feeder reads a `BATCH_SIZE` of data each time and feed them to the training/testing process.
Next we define data feeders for test and train. The feeder reads a `buf_size` of data each time and feed them to the training/testing process.
`paddle.dataset.movielens.train` will yield records during each pass, after shuffling, a batch input of `buf_size` is generated for training.
`paddle.dataset.movielens.train` will yield records during each pass, after shuffling, a batch input of `BATCH_SIZE` is generated for training.
```python
```python
train_reader=paddle.batch(
train_reader=paddle.batch(
...
@@ -373,11 +378,11 @@ test_reader = paddle.batch(
...
@@ -373,11 +378,11 @@ test_reader = paddle.batch(
### Create Trainer
### Create Trainer
Create a trainer that takes `train_program` as input and specifies optimizer.
Create a trainer that takes `train_program` as input and specify optimizer function.
Use create_lod_tensor(data, lod, place) API to generate LoD Tensor, where `data` is a list of sequences of index numbers, `lod` is the level of detail (lod) info associated with `data`.
Use create_lod_tensor(data, lod, place) API to generate LoD Tensor, where `data` is a list of sequences of index numbers, `lod` is the level of detail (lod) info associated with `data`.
For example, data = [[10, 2, 3], [2, 3]] means that it contains two sequences of indexes, of length 3 and 2, respectively.
For example, data = [[10, 2, 3], [2, 3]] means that it contains two sequences of indices, of length 3 and 2, respectively.
Correspondingly, lod = [[3, 2]] contains one level of detail info, indicating that `data` consists of two sequences of length 3 and 2.
Correspondingly, lod = [[3, 2]] contains one level of detail info, indicating that `data` consists of two sequences of length 3 and 2.