Discriminative model vs Generative model

本文对比了判别模型和生成模型的区别。判别模型关注的是在给定特征下目标出现的概率,通常用于分类任务。生成模型则侧重于特征与目标间的联合分布,能更好地模拟数据生成过程。

Discriminative model(判别模型): 

对p(y|x)建模,只能计算在给定特征x情况下,目标y出现的概率。可以认为是对特征x的判断,故称为判别模型。一般都是监督训练,很难改造成无监督的。在分类问题上可能比generative model好一些。


Generative model(生成模型): 

对p(x|y)和p(y)建模,利用这个模型,我们可以模拟生成各个特征数据,故而称为生成模型。可以对变量之间的复杂关系建模,一般比discriminative model性能好。


举个例子,假设我们的特征是“颜色”、“味道”、“形状”,目标是“葡萄”和“香蕉”。那么训练得到的判别模型会告诉我们:“颜色=红色,味道=酸,形状=圆”的样本是葡萄的概率是0.8,而“颜色=黄色,味道=甜,形状=长”的是香蕉的概率是0.9;而生成模型会告诉你:如果是葡萄,那“颜色=黄/红”的概率分别是多少,“味道=酸/甜”的概率又分别是多少。
# Get a batch of real data. Our goal is to make data that looks like this. def get_real_data_batch(n_sample): np.random.seed(0) x_true = np.random.normal(size=(1,n_sample)) + 7.5 return x_true # This is our generator -- takes the single parameter theta # of the generative model and generates n samples def generator(z, theta): x_gen = z + theta return x_gen # Define our discriminative model # Logistic sigmoid, maps from [-infty,infty] to [0,1] def sig(data_in): return 1.0 / (1.0+np.exp(-data_in)) # Discriminator computes y def discriminator(x, phi0, phi1): return sig(phi0 + phi1 * x) # Draws a figure like Figure 15.1a def draw_data_model(x_real, x_syn, phi0=None, phi1=None): fix, ax = plt.subplots(); for x in x_syn: ax.plot([x,x],[0,0.33],color='#f47a60') for x in x_real: ax.plot([x,x],[0,0.33],color='#7fe7dc') if phi0 is not None: x_model = np.arange(0,10,0.01) y_model = discriminator(x_model, phi0, phi1) ax.plot(x_model, y_model,color='#dddddd') ax.set_xlim([0,10]) ax.set_ylim([0,1]) plt.show() # Get data batch x_real = get_real_data_batch(10) # Initialize generator and synthesize a batch of examples theta = 3.0 np.random.seed(1) z = np.random.normal(size=(1,10)) x_syn = generator(z, theta) # Initialize discriminator model phi0 = -2 phi1 = 1 draw_data_model(x_real, x_syn, phi0, phi1) def compute_generator_loss(z, theta, phi0, phi1): # TODO-2 -- Run the generator on the latent variables z with the parameters theta # to generate new data x_syn # Then run the discriminator on the new data to get the probability of being real # The loss is the total negative log probability of being synthesized (i.e. of not being real) # Replace this code return loss # Test generator loss to check you have it correct loss = compute_generator_loss(z, theta, -2, 1) print("True Loss = 13.78437035945412, Your loss=", loss ) 帮我完成一下
12-11
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值