-
Notifications
You must be signed in to change notification settings - Fork 11
/
cfgan.py
47 lines (44 loc) · 1.43 KB
/
cfgan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# -*- coding: utf-8 -*-
"""
Author:
Xuxin Zhang,xuxinz@qq.com
Reference: Chae D K , Kang J S , Kim S W , et al.
CFGAN: A Generic Collaborative Filtering Framework based on Generative Adversarial Networks[C]// the 27th ACM International Conference. ACM, 2018.
"""
import torch
import torch.nn as nn
class discriminator(nn.Module):
def __init__(self,itemCount,info_shape):
super(discriminator,self).__init__()
self.dis=nn.Sequential(
nn.Linear(itemCount+info_shape,1024),
nn.ReLU(True),
nn.Linear(1024,128),
nn.ReLU(True),
nn.Linear(128,16),
nn.ReLU(True),
nn.Linear(16,1),
nn.Sigmoid()
)
def forward(self,data,condition):
data_c = torch.cat((data,condition),1)
result=self.dis( data_c )
return result
class generator(nn.Module):
def __init__(self,itemCount,info_shape):
self.itemCount = itemCount
super(generator,self).__init__()
self.gen=nn.Sequential(
nn.Linear(self.itemCount+info_shape, 256),
nn.ReLU(True),
nn.Linear(256, 512),
nn.ReLU(True),
nn.Linear(512,1024),
nn.ReLU(True),
nn.Linear(1024, itemCount),
nn.Sigmoid()
)
def forward(self,noise,useInfo):
G_input = torch.cat([noise, useInfo], 1)
result=self.gen(G_input)
return result