0

I'm a beginner in code and Python. I want to see how this code (RBM) works. And I try to input data from 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)

But i get this message when running.

File "C:/Users/USER/cx.py", line 39, in train

num_examples = data.shape[0]

AttributeError: 'str' object has no attribute 'shape'

What should I do?

Community
  • 1
  • 1
V.O Vian
  • 53
  • 1
  • 10
  • Your `training_data` is not training data. It is the name of an Excel file. The training data is somewhere in `sheet[]`. That's what you should pass to `r.train()`. – DYZ Aug 21 '17 at 04:51
  • Actually the original code is like this ' training_data = np.array([[1,1,1,0,0,0],[1,0,1,0,0,0],[1,1,1,0,0,0],[0,0,1,1,1,0], [0,0,1,1,0,0],[0,0,1,1,1,0]]) r.train(training_data, max_epochs = 5000) ' But I try to retrieve data from columns in excel as much as 261 lines. So I am confused, how to make the code. – V.O Vian Aug 21 '17 at 05:02

0 Answers0