Bayes optimal

本文探讨了贝叶斯最优决策的概念及其在机器学习、模式识别领域的应用。通过生动的例子解释了理论(模型)在问题空间中的作用,并对比了不同决策方法如最大后验概率(MAP)与最大似然估计(MLE)的区别。

as its name, bayes optimal decision is the best decision over all decisions, however it is impossible to working out solutions because it requires to summing out all hypothese. you can't obtain ALL hypothese.

in <<Machine Learning>>, its definition is:

in <<Pattern Recognition>>:

 

in <<Foundations of Statistical Natural Language Processing>>:

"Suppose that we did not actually see the sequence of coin tosses but just heard the results shouted out over the fence. Now it may be the case, as we have assumed so far, that the results reported truly reflect the results of tossing a single, possibly weighted coin. This is the theory 'u' which is a family of models, with a parameter representing the weighting of the coin. But an alternative theory is that at each step someone is tossing two fair coins, and calling out "tails" if both of them come down tails, and heads otherwise."

this explanation is very vivid. hypothese or parameters is a theory(model) in problem space. following is some metaphors:

ideal categorizations about texts ------- methods(hypothese; theory; modal): SVM, KNN, Bayes... ------- a specified category

mathematical reasoning ------- programming languages ------- codes

thinkings ------- natural languages ------- text or speech

probability distribution about pattern recognition decision ------- kinds of distribution & parameters for them ------- a decision

M.A.P (P(x|a)P(a)) ------- M.L (P(x|a)) ------- result (x)

speech ------- HMM ------- meaning underlying natural language

a concept in world ------- operations of features ------- a pattern

 

import matplotlib.pyplot as plt import setup_problem from sklearn.base import BaseEstimator, RegressorMixin import numpy as np import nodes import graph import plot_utils #import pdb #pdb.set_trace() #useful for debugging! class MLPRegression(BaseEstimator, RegressorMixin): """ 基于计算图的MLP实现 """ def __init__(self, num_hidden_units=10, step_size=.005, init_param_scale=0.01, max_num_epochs = 5000): self.num_hidden_units = num_hidden_units self.init_param_scale = 0.01 self.max_num_epochs = max_num_epochs self.step_size = step_size # 开始构建计算图 self.x = nodes.ValueNode(node_name="x") # to hold a vector input self.y = nodes.ValueNode(node_name="y") # to hold a scalar response # 参数初始化节点 self.W1 = nodes.ValueNode("W1") self.b1 = nodes.ValueNode("b1") self.W2 = nodes.ValueNode("W2") self.b2 = nodes.ValueNode("b2") # 隐藏层计算 self.affine1 = nodes.AffineNode(self.W1, self.x, self.b1, "affine1") self.tanh = nodes.TanhNode(self.affine1, "tanh") # 输出层计算 self.affine2 = nodes.AffineNode(self.W2, self.tanh, self.b2, "affine2") self.prediction = self.affine2 # 损失函数计算 self.loss = nodes.SquaredL2DistanceNode(self.prediction, self.y, "loss") # 正则化项 self.reg_W1 = nodes.L2NormPenaltyNode(l2_reg=0.01, w=self.W1, node_name="reg_W1") self.reg_W2 = nodes.L2NormPenaltyNode(l2_reg=0.01, w=self.W2, node_name="reg_W2") self.total_loss = nodes.SumNode(self.loss, self.reg_W1, "total_loss") self.objective = nodes.SumNode(self.total_loss, self.reg_W2, "objective") # 构建计算图 self.graph = graph.ComputationGraphFunction( inputs=[self.x], outcomes=[self.y], parameters=[self.W1, self.b1, self.W2, self.b2], prediction=self.prediction, objective=self.objective ) ## TODO ## Hint: 根据PPT中给定的图,来构建MLP def fit(self, X, y): num_instances, num_ftrs = X.shape y = y.reshape(-1) ## TODO: 初始化参数(小的随机数——不是全部为0,以打破对称性) s = self.init_param_scale init_values = None ## TODO,在这里进行初始化,hint:调用np.random.standard_normal方法 self.graph.set_parameters(init_values) for epoch in range(self.max_num_epochs): shuffle = np.random.permutation(num_instances) epoch_obj_tot = 0.0 for j in shuffle: obj, grads = self.graph.get_gradients(input_values = {"x": X[j]}, outcome_values = {"y": y[j]}) #print(obj) epoch_obj_tot += obj # Take step in negative gradient direction steps = {} for param_name in grads: steps[param_name] = -self.step_size * grads[param_name] self.graph.increment_parameters(steps) if epoch % 50 == 0: train_loss = sum((y - self.predict(X,y)) **2)/num_instances print("Epoch ",epoch,": Ave objective=",epoch_obj_tot/num_instances," Ave training loss: ",train_loss) def predict(self, X, y=None): try: getattr(self, "graph") except AttributeError: raise RuntimeError("You must train classifer before predicting data!") num_instances = X.shape[0] preds = np.zeros(num_instances) for j in range(num_instances): preds[j] = self.graph.get_prediction(input_values={"x":X[j]}) return preds def main(): #lasso_data_fname = "lasso_data.pickle" lasso_data_fname = r"C:\Users\XM_Ta\OneDrive\Desktop\1120223544-汤阳光-实验四\Question\lasso_data.pickle" x_train, y_train, x_val, y_val, target_fn, coefs_true, featurize = setup_problem.load_problem(lasso_data_fname) # Generate features X_train = featurize(x_train) X_val = featurize(x_val) # Let's plot prediction functions and compare coefficients for several fits # and the target function. pred_fns = [] x = np.sort(np.concatenate([np.arange(0,1,.001), x_train])) pred_fns.append({"name": "Target Parameter Values (i.e. Bayes Optimal)", "coefs": coefs_true, "preds": target_fn(x)}) estimator = MLPRegression(num_hidden_units=10, step_size=0.001, init_param_scale=.0005, max_num_epochs=5000) x_train_as_column_vector = x_train.reshape(x_train.shape[0],1) # fit expects a 2-dim array x_as_column_vector = x.reshape(x.shape[0],1) # fit expects a 2-dim array estimator.fit(x_train_as_column_vector, y_train) name = "MLP regression - no features" pred_fns.append({"name":name, "preds": estimator.predict(x_as_column_vector) }) X = featurize(x) estimator = MLPRegression(num_hidden_units=10, step_size=0.0005, init_param_scale=.01, max_num_epochs=500) estimator.fit(X_train, y_train) name = "MLP regression - with features" pred_fns.append({"name":name, "preds": estimator.predict(X) }) plot_utils.plot_prediction_functions(x, pred_fns, x_train, y_train, legend_loc="best") if __name__ == '__main__': main() 请帮我补充上述代码
05-29
标题基于Python的自主学习系统后端设计与实现AI更换标题第1章引言介绍自主学习系统的研究背景、意义、现状以及本文的研究方法和创新点。1.1研究背景与意义阐述自主学习系统在教育技术领域的重要性和应用价值。1.2国内外研究现状分析国内外在自主学习系统后端技术方面的研究进展。1.3研究方法与创新点概述本文采用Python技术栈的设计方法和系统创新点。第2章相关理论与技术总结自主学习系统后端开发的相关理论和技术基础。2.1自主学习系统理论阐述自主学习系统的定义、特征和理论基础。2.2Python后端技术栈介绍DjangoFlask等Python后端框架及其适用场景。2.3数据库技术讨论关系型和非关系型数据库在系统中的应用方案。第3章系统设计与实现详细介绍自主学习系统后端的设计方案和实现过程。3.1系统架构设计提出基于微服务的系统架构设计方案。3.2核心模块设计详细说明用户管理、学习资源管理、进度跟踪等核心模块设计。3.3关键技术实现阐述个性化推荐算法、学习行为分析等关键技术的实现。第4章系统测试与评估对系统进行功能测试和性能评估。4.1测试环境与方法介绍测试环境配置和采用的测试方法。4.2功能测试结果展示各功能模块的测试结果和问题修复情况。4.3性能评估分析分析系统在高并发等场景下的性能表现。第5章结论与展望总结研究成果并提出未来改进方向。5.1研究结论概括系统设计的主要成果和技术创新。5.2未来展望指出系统局限性并提出后续优化方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值