# Python——受限玻尔兹曼机

from __future__ import print_function
import random
import numpy as np
import xlrd
import xlwt

class RBM:
def __init__(self, num_visible, num_hidden):#初始化  可见单元和隐藏单元个数
self.num_hidden = num_hidden
self.num_visible = num_visible
self.debug_print = True

# Initialize a weight matrix, of dimensions (num_visible x num_hidden), using
# a uniform distribution between -sqrt(6. / (num_hidden + num_visible))
# and sqrt(6. / (num_hidden + num_visible)). One could vary the
# standard deviation by multiplying the interval with appropriate value.
# Here we initialize the weights with mean 0 and standard deviation 0.1.
# Reference: Understanding the difficulty of training deep feedforward
# neural networks by Xavier Glorot and Yoshua Bengio
np_rng = np.random.RandomState(1234)

self.weights = np.asarray(np_rng.uniform(
low=-0.1 * np.sqrt(6. / (num_hidden + num_visible)),
high=0.1 * np.sqrt(6. / (num_hidden + num_visible)),
size=(num_visible, num_hidden)))

# Insert weights for the bias units into the first row and first column.
self.weights = np.insert(self.weights, 0, 0, axis=0)
self.weights = np.insert(self.weights, 0, 0, axis=1)

def train(self, data, max_epochs=1000, learning_rate=0.1):#训练  数据   迭代次数   学习率
"""
Train the machine.

Parameters
----------
data: A matrix where each row is a training example consisting of the states of visible units.
"""

num_examples = data.shape[0]

# Insert bias units of 1 into the first column.
data = np.insert(data, 0, 1, axis=1)

for epoch in range(max_epochs):
# Clamp to the data and sample from the hidden units.
# (This is the "positive CD phase", aka the reality phase.)
pos_hidden_activations = np.dot(data, self.weights)
pos_hidden_probs = self._logistic(pos_hidden_activations)
pos_hidden_probs[:, 0] = 1  # Fix the bias unit.
pos_hidden_states = pos_hidden_probs > np.random.rand(num_examples, self.num_hidden + 1)
# Note that we're using the activation *probabilities* of the hidden states, not the hidden states
# themselves, when computing associations. We could also use the states; see section 3 of Hinton's
# "A Practical Guide to Training Restricted Boltzmann Machines" for more.
pos_associations = np.dot(data.T, pos_hidden_probs)

# Reconstruct the visible units and sample again from the hidden units.
# (This is the "negative CD phase", aka the daydreaming phase.)
neg_visible_activations = np.dot(pos_hidden_states, self.weights.T)
neg_visible_probs = self._logistic(neg_visible_activations)
neg_visible_probs[:, 0] = 1  # Fix the bias unit.
neg_hidden_activations = np.dot(neg_visible_probs, self.weights)
neg_hidden_probs = self._logistic(neg_hidden_activations)
# Note, again, that we're using the activation *probabilities* when computing associations, not the states
# themselves.
neg_associations = np.dot(neg_visible_probs.T, neg_hidden_probs)

# Update weights.
self.weights += learning_rate * ((pos_associations - neg_associations) / num_examples)

error = np.sum((data - neg_visible_probs) ** 2)
if self.debug_print:
print("Epoch %s: error is %s" % (epoch, error))

def run_visible(self, data):#根据模型  将原始数据转换为预训练数据
"""
Assuming the RBM has been trained (so that weights for the network have been learned),
run the network on a set of visible units, to get a sample of the hidden units.

Parameters
----------
data: A matrix where each row consists of the states of the visible units.

Returns
-------
hidden_states: A matrix where each row consists of the hidden units activated from the visible
units in the data matrix passed in.
"""

num_examples = data.shape[0]

# Create a matrix, where each row is to be the hidden units (plus a bias unit)
# sampled from a training example.
hidden_states = np.ones((num_examples, self.num_hidden + 1))

# Insert bias units of 1 into the first column of data.
data = np.insert(data, 0, 1, axis=1)

# Calculate the activations of the hidden units.
hidden_activations = np.dot(data, self.weights)
# Calculate the probabilities of turning the hidden units on.
hidden_probs = self._logistic(hidden_activations)
# Turn the hidden units on with their specified probabilities.
hidden_states[:, :] = hidden_probs > np.random.rand(num_examples, self.num_hidden + 1)
# Always fix the bias unit to 1.
# hidden_states[:,0] = 1

# Ignore the bias units.
hidden_states = hidden_states[:, 1:]
return hidden_states

def run_hidden(self, data):#将预训练数据转换为原始数据
"""
Assuming the RBM has been trained (so that weights for the network have been learned),
run the network on a set of hidden units, to get a sample of the visible units.

Parameters
----------
data: A matrix where each row consists of the states of the hidden units.

Returns
-------
visible_states: A matrix where each row consists of the visible units activated from the hidden
units in the data matrix passed in.
"""

num_examples = data.shape[0]

# Create a matrix, where each row is to be the visible units (plus a bias unit)
# sampled from a training example.
visible_states = np.ones((num_examples, self.num_visible + 1))

# Insert bias units of 1 into the first column of data.
data = np.insert(data, 0, 1, axis=1)

# Calculate the activations of the visible units.
visible_activations = np.dot(data, self.weights.T)
# Calculate the probabilities of turning the visible units on.
visible_probs = self._logistic(visible_activations)
# Turn the visible units on with their specified probabilities.
visible_states[:, :] = visible_probs > np.random.rand(num_examples, self.num_visible + 1)
# Always fix the bias unit to 1.
# visible_states[:,0] = 1

# Ignore the bias units.
visible_states = visible_states[:, 1:]
return visible_states

def daydream(self, num_samples):#概率  因为输出值只能是0和1  所以要进行抽样估计
"""
Randomly initialize the visible units once, and start running alternating Gibbs sampling steps
(where each step consists of updating all the hidden units, and then updating all of the visible units),
taking a sample of the visible units at each step.
Note that we only initialize the network *once*, so these samples are correlated.

Returns
-------
samples: A matrix, where each row is a sample of the visible units produced while the network was
daydreaming.
"""

# Create a matrix, where each row is to be a sample of of the visible units
# (with an extra bias unit), initialized to all ones.
samples = np.ones((num_samples, self.num_visible + 1))

# Take the first sample from a uniform distribution.
samples[0, 1:] = np.random.rand(self.num_visible)

# Start the alternating Gibbs sampling.
# Note that we keep the hidden units binary states, but leave the
# visible units as real probabilities. See section 3 of Hinton's
# "A Practical Guide to Training Restricted Boltzmann Machines"
# for more on why.
for i in range(1, num_samples):
visible = samples[i - 1, :]

# Calculate the activations of the hidden units.
hidden_activations = np.dot(visible, self.weights)
# Calculate the probabilities of turning the hidden units on.
hidden_probs = self._logistic(hidden_activations)
# Turn the hidden units on with their specified probabilities.
hidden_states = hidden_probs > np.random.rand(self.num_hidden + 1)
# Always fix the bias unit to 1.
hidden_states[0] = 1

# Recalculate the probabilities that the visible units are on.
visible_activations = np.dot(hidden_states, self.weights.T)
visible_probs = self._logistic(visible_activations)
visible_states = visible_probs > np.random.rand(self.num_visible + 1)
samples[i, :] = visible_states
# Ignore the bias units (the first column), since they're always set to 1.
return samples[:, 1:]

def _logistic(self, x):#sigmoid
return 1.0 / (1 + np.exp(-x))
def excel2m(self,path):
data = xlrd.open_workbook(path)
table = data.sheets()[0]
nrows = table.nrows  # 行数
ncols = table.ncols  # 列数
c1 = np.arange(0, nrows, 1)
datamatrix = np.zeros((nrows, ncols))
for x in range(ncols):
cols = table.col_values(x)
minVals = min(cols)
maxVals = max(cols)
cols1 = np.matrix(cols)  # 把list转换为矩阵进行矩阵操作
#ranges = maxVals - minVals
#b = cols1 - minVals
#normcols = b / ranges  # 数据进行归一化处理
datamatrix[:, x] = cols1  # 把数据进行存储
return datamatrix
def save(self,data,path):
f = xlwt.Workbook()  # 创建工作簿
sheet1 = f.add_sheet(u'sheet1', cell_overwrite_ok=True)  # 创建sheet
[h, l] = data.shape  # h为行数，l为列数
for i in range(h):
for j in range(l):
sheet1.write(i, j, data[i, j])
f.save(path)

if __name__ == '__main__':
'''
第一层：输入特征数为可见单元  设置隐藏单元数
'''
r = RBM(num_visible=88, num_hidden=40)#隐藏单元与可见单元数
training_data = np.array(r.excel2m(r'train.xlsx'))#训练数据处理为矩阵
r.train(training_data, max_epochs=10000,learning_rate=0.01)#开始训练
#r.save(r.weights,'weight_layer1.xls')#保存权重及偏置量
user = np.array(r.excel2m(r'test.xlsx'))
r.save(r.run_visible(user),'test.xls')#保存第一层输出

user = np.array(r.excel2m(r'train.xlsx'))
r.save(r.run_visible(user), 'train.xls')  # 保存第一层输出