我是代码和 Python 的初学者。我想看看这段代码(RBM)是如何工作的。我尝试从excel输入数据。
from __future__ import print_function
import numpy as np
import openpyxl
class RBM:
def __init__(self, num_visible, num_hidden):
self.num_hidden = num_hidden
self.num_visible = num_visible
self.debug_print = True
np_rng = np.random.RandomState(1234)
self.weights = np.asarray(np_rng.uniform(
low=-0.1 * np.sqrt(261. / (num_hidden + num_visible)),
high=0.1 * np.sqrt(261. / (num_hidden + num_visible)),
size=(num_visible, num_hidden)))
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)
....
# Ignore the bias units (the first column), since they're always set to 1.
return samples[:,1:]
def _logistic(self, x):
return 1.0 / (1 + np.exp(-x))
if __name__ == '__main__':
r = RBM(num_visible = 1, num_hidden = 261)
training_data = 'data1.xlsx'
wb = openpyxl.load_workbook(training_data)
sheet = wb.get_sheet_by_name('Sheet1')
for rowOfCellObjects in sheet['f2':'f262']:
for cellObj in rowOfCellObjects:
print(cellObj.coordinate, cellObj.value)
r.train(training_data, max_epochs = 5000)
print(r.weights)
print(r.run_visible)
但是我在运行时收到此消息。
文件“C:/Users/USER/cx.py”,第 39 行,在火车中
num_examples = data.shape[0]
AttributeError:“str”对象没有属性“shape”
我应该怎么办?