簡體   English   中英

使用神經網絡進行Python查詢

[英]Python inquiry using a neural network

我正在嘗試修改由軟件開發人員(Kyle Dickerson)編寫的代碼,並將其編寫如下:

所以我有這段代碼:

from __future__ import division

## Kyle Dickerson
## kyle.dickerson@gmail.com
## Jan 15, 2008
##
## Self-organizing map using scipy
## This code is licensed and released under the GNU GPL

## This code uses a square grid rather than hexagonal grid, as scipy    allows for fast square grid computation.
## I designed sompy for speed, so attempting to read the code may not be  very intuitive.
## If you're trying to learn how SOMs work, I would suggest starting with     Paras Chopras SOMPython code:
##  http://www.paraschopra.com/sourcecode/SOM/index.php
## It has a more intuitive structure for those unfamiliar  with scipy, however it is much slower.

## If you do use this code for something, please  let me know, I'd like to know if has been useful to anyone.

from random import *
from math import *
import sys
import scipy
import numpy
class SOM:

def __init__(self, height=4, width=4, FV_size=3, learning_rate=0.005):
    self.height = height
    self.width = width
    self.FV_size = FV_size
    self.radius = (height+width)/3
    self.learning_rate = learning_rate
    self.nodes = scipy.array([[ [random()*255 for 
     i in range(FV_size)]  for x in range(width)] for y in range(height)])


    self.nodes = scipy.array([[1,2,3],[4,5,6],[4,5,6],
   [4,5,6],[4,5,6],    [4,5,6],[4,5,6],[4,5,6],[4,5,6],
   [4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6]])



    print "SOM",self.nodes

  def train(self, iterations=1000, train_vector=[[]]):
    for t in range(len(train_vector)):
        train_vector[t] = scipy.array(train_vector[t])
    print "training",train_vector[t],t
    time_constant = iterations/log(self.radius)
    delta_nodes = scipy.array([[[0 for i in range(self.FV_size)] 
    for x in range(self.width)] for y in range(self.height)])

    for i in range(1, iterations+1):
        delta_nodes.fill(0)
        radius_decaying=self.radius*exp(-1.0*i/time_constant)
        rad_div_val = 2 * radius_decaying * i
                                                                                     learning_rate_decaying=self.learning_rate*exp(-1.0*i/time_constant)
         sys.stdout.write("\rTraining Iteration: 
       " + str(i) + "/" +  str(iterations))

        for j in range(len(train_vector)):
            best = self.best_match(train_vector[j])
            for loc in self.find_neighborhood(best, radius_decaying):
                influence = exp( (-1.0 * (loc[2]**2)) / rad_div_val)
                inf_lrd = influence*learning_rate_decaying

                delta_nodes[loc[0],loc[1]] += inf_lrd*
       (train_vector[j]-  self.nodes[loc[0],loc[1]])
        self.nodes += delta_nodes
    sys.stdout.write("\n")
 # Returns a list of points which live within 'dist' of 'pt'
 # Uses the Chessboard distance
  # pt is (row, column)
  def find_neighborhood(self, pt, dist):
    min_y = max(int(pt[0] - dist), 0)
    max_y = min(int(pt[0] + dist), self.height)
    min_x = max(int(pt[1] - dist), 0)
    max_x = min(int(pt[1] + dist), self.width)
    neighbors = []
    for y in range(min_y, max_y):
        for x in range(min_x, max_x):
            dist = abs(y-pt[0]) + abs(x-pt[1])
            neighbors.append((y,x,dist))
    return neighbors

# Returns location of best match, uses Euclidean distance
# target_FV is a scipy array
def best_match(self, target_FV):
    loc = scipy.argmin((((self.nodes - target_FV)**2).sum(axis=2))**0.5)
    r = 0
    while loc > self.width:
        loc -= self.width
        r += 1
    c = loc
    return (r, c)

# returns the Euclidean distance between two Feature Vectors
# FV_1, FV_2 are scipy arrays
def FV_distance(self, FV_1, FV_2):
    return (sum((FV_1 - FV_2)**2))**0.5

 if __name__ == "__main__":
     print "Initialization..."

      colors = [ [0, 0, 0], [0, 0, 255], [0, 255, 0], 
          [0, 255, 255], [255, 0,  0], [255, 0, 255],  
         [255, 255, 0], [255, 255, 255]]
      width = 32
     height = 32
     color_som = SOM(width,height,3,0.05)
     print "Training colors..."
     color_som.train(1000, colors)
     try:
         from PIL import Image
         print "Saving Image: sompy_test_colors.png..."
         img = Image.new("RGB", (width, height))
         for r in range(height):
             for c in range(width):
                   img.putpixel((c,r),(int(color_som.nodes[r,c,0]),  
                        int(color_som.nodes[r,c,1]),       int(color_som.nodes[r,c,2])))
         print "color nodes",color_som.nodes
         img = img.resize((width*10, height*10),Image.NEAREST)
         img.save("sompy_test_colors.png")
    except:
         print "Error saving the image, do you have PIL (Python Imaging       Library) installed?"

但是當我嘗試從

self.nodes = scipy.array([[ [random()*255 
     for i in range(FV_size)] for x in range(width)] 
     for y in range(height)])

在原始代碼中是這樣的:

     self.nodes = scipy.array([[1,2,3],[4,5,6],[4,5,6],
       [4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],
     [4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6]])

我收到錯誤消息:

File "sompy5.py", line 112, in <module>
color_som.train(1000, colors)
File "sompy5.py", line 65, in train
best = self.best_match(train_vector[j])
 File "sompy5.py", line 92, in best_match
loc = scipy.argmin((((self.nodes - target_FV)**2).sum(axis=2))**0.5)
 File "/usr/lib/python2.7/dist-packages/numpy/core/_methods.py", 
 line 25, in   _sum
  out=out, keepdims=keepdims)
  ValueError: 'axis' entry is out of bounds

是否需要做一些事情才能使向量匹配?

這部分是一個3-D數組(以3個方括號開頭的參數):

self.nodes = scipy.array([[ [random()*255 for 
 i in range(FV_size)]  for x in range(width)] for y in range(height)])

這部分是二維數組:

self.nodes = scipy.array([[1,2,3],[4,5,6],[4,5,6],
   [4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],
 [4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6],[4,5,6]])

因此,您需要將self.nodes轉換為適當的3-D數組。

編輯:所需語法的示例:

self.nodes = scipy.array([[ [1,2,3],[4,5,6]] , [[7,8,9],[10,11,12]]])

print(self.nodes)
>>> array([[[ 1, 2, 3],
            [ 4, 5, 6]],

           [[ 7, 8, 9],
            [10, 11, 12]]])

編輯2:

另一個選擇是建立一個線性數組,然后reshape():

myarray = scipy.array([1,2,3,4,5,6,7,8,9,10,11,12])
myarray = myarray.reshape( (2, 2, 3) )  ## 3 numbers for 3 dimensions, but the product must be the same as the number of elements of the original array
print(myarray)
>>> array([[[ 1, 2, 3],
            [ 4, 5, 6]],

           [[ 7, 8, 9],
            [10, 11, 12]]])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM