内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

softmax及python實現(xiàn)過程解析-創(chuàng)新互聯(lián)

相對于自適應神經網絡、感知器,softmax巧妙低使用簡單的方法來實現(xiàn)多分類問題。

創(chuàng)新互聯(lián)公司服務項目包括內丘網站建設、內丘網站制作、內丘網頁制作以及內丘網絡營銷策劃等。多年來,我們專注于互聯(lián)網行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構等提供互聯(lián)網行業(yè)的解決方案,內丘網站推廣取得了明顯的社會效益與經濟效益。目前,我們服務的客戶以成都為中心已經輻射到內丘省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!
  • 功能上,完成從N維向量到M維向量的映射
  • 輸出的結果范圍是[0, 1],對于一個sample的結果所有輸出總和等于1
  • 輸出結果,可以隱含地表達該類別的概率

softmax的損失函數是采用了多分類問題中常見的交叉熵,注意經常有2個表達的形式

  • 經典的交叉熵形式:L=-sum(y_right * log(y_pred)), 具體
  • 簡單版本是: L = -Log(y_pred),具體

這兩個版本在求導過程有點不同,但是結果都是一樣的,同時損失表達的意思也是相同的,因為在第一種表達形式中,當y不是

正確分類時,y_right等于0,當y是正確分類時,y_right等于1。

下面基于mnist數據做了一個多分類的實驗,整體能達到85%的精度。

'''
softmax classifier for mnist 

created on 2019.9.28
author: vince
'''
import math
import logging
import numpy 
import random
import matplotlib.pyplot as plt
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets
from sklearn.metrics import accuracy_score

def loss_max_right_class_prob(predictions, y):
	return -predictions[numpy.argmax(y)];

def loss_cross_entropy(predictions, y):
	return -numpy.dot(y, numpy.log(predictions));
	
'''
Softmax classifier
linear classifier 
'''
class Softmax:

	def __init__(self, iter_num = 100000, batch_size = 1):
		self.__iter_num = iter_num;
		self.__batch_size = batch_size;
	
	def train(self, train_X, train_Y):
		X = numpy.c_[train_X, numpy.ones(train_X.shape[0])];
		Y = numpy.copy(train_Y);

		self.L = [];

		#initialize parameters
		self.__weight = numpy.random.rand(X.shape[1], 10) * 2 - 1.0;
		self.__step_len = 1e-3; 

		logging.info("weight:%s" % (self.__weight));

		for iter_index in range(self.__iter_num):
			if iter_index % 1000 == 0:
				logging.info("-----iter:%s-----" % (iter_index));
			if iter_index % 100 == 0:
				l = 0;
				for i in range(0, len(X), 100):
					predictions = self.forward_pass(X[i]);
					#l += loss_max_right_class_prob(predictions, Y[i]); 
					l += loss_cross_entropy(predictions, Y[i]); 
				l /= len(X);
				self.L.append(l);

			sample_index = random.randint(0, len(X) - 1);
			logging.debug("-----select sample %s-----" % (sample_index));

			z = numpy.dot(X[sample_index], self.__weight);
			z = z - numpy.max(z);
			predictions = numpy.exp(z) / numpy.sum(numpy.exp(z));
			dw = self.__step_len * X[sample_index].reshape(-1, 1).dot((predictions - Y[sample_index]).reshape(1, -1));
#			dw = self.__step_len * X[sample_index].reshape(-1, 1).dot(predictions.reshape(1, -1)); 
#			dw[range(X.shape[1]), numpy.argmax(Y[sample_index])] -= X[sample_index] * self.__step_len;

			self.__weight -= dw;

			logging.debug("weight:%s" % (self.__weight));
			logging.debug("loss:%s" % (l));
		logging.info("weight:%s" % (self.__weight));
		logging.info("L:%s" % (self.L));
	
	def forward_pass(self, x):
		net = numpy.dot(x, self.__weight);
		net = net - numpy.max(net);
		net = numpy.exp(net) / numpy.sum(numpy.exp(net)); 
		return net;

	def predict(self, x):
		x = numpy.append(x, 1.0);
		return self.forward_pass(x);


def main():
	logging.basicConfig(level = logging.INFO,
			format = '%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
			datefmt = '%a, %d %b %Y %H:%M:%S');
			
	logging.info("trainning begin.");

	mnist = read_data_sets('../data/MNIST',one_hot=True)  # MNIST_data指的是存放數據的文件夾路徑,one_hot=True 為采用one_hot的編碼方式編碼標簽

	#load data
	train_X = mnist.train.images        #訓練集樣本
	validation_X = mnist.validation.images   #驗證集樣本
	test_X = mnist.test.images         #測試集樣本
	#labels
	train_Y = mnist.train.labels        #訓練集標簽
	validation_Y = mnist.validation.labels   #驗證集標簽
	test_Y = mnist.test.labels         #測試集標簽

	classifier = Softmax();
	classifier.train(train_X, train_Y);

	logging.info("trainning end. predict begin.");

	test_predict = numpy.array([]);
	test_right = numpy.array([]);
	for i in range(len(test_X)):
		predict_label = numpy.argmax(classifier.predict(test_X[i]));
		test_predict = numpy.append(test_predict, predict_label);
		right_label = numpy.argmax(test_Y[i]);
		test_right = numpy.append(test_right, right_label);

	logging.info("right:%s, predict:%s" % (test_right, test_predict));
	score = accuracy_score(test_right, test_predict);
	logging.info("The accruacy score is: %s "% (str(score)));


	plt.plot(classifier.L)
	plt.show();

if __name__ == "__main__":
	main();

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

分享題目:softmax及python實現(xiàn)過程解析-創(chuàng)新互聯(lián)
網址分享:http://www.rwnh.cn/article20/ceigco.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供定制開發(fā)網站營銷、商城網站、網站制作網站建設、移動網站建設

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網頁設計公司
华坪县| 岳普湖县| 锡林郭勒盟| 湛江市| 璧山县| 天全县| 郎溪县| 南阳市| 阿尔山市| 延津县| 定远县| 东兰县| 拜泉县| 湖南省| 白河县| 永清县| 英山县| 凌云县| 措美县| 邹城市| 潮州市| 常宁市| 通山县| 三河市| 上栗县| 湄潭县| 沈丘县| 容城县| 平顶山市| 通辽市| 赤水市| 抚远县| 晴隆县| 隆安县| 西峡县| 济宁市| 乐平市| 隆昌县| 朝阳区| 奇台县| 东宁县|