Code Monkey home page Code Monkey logo

kerasfuzzy's People

Contributors

kenoma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

kerasfuzzy's Issues

how i build this rule layer in keras without error?

I want build this custom Layer in keras to use it in sequential model. While running the code i am getting an error.can someone help me?? Below you can see the Code and the error.

class WeightedLayer(Layer):
  def __init__(self, n_input, n_memb, **kwargs):
    super(WeightedLayer, self).__init__( **kwargs)
    self.n = n_input   # 16 features
    self.m = n_memb    # 3

  def build(self, batch_input_shape):
    super(WeightedLayer, self).build(batch_input_shape)

  def call(self, input_):
    CP = []
    self.batch_size = tf.shape(input_)[0]
    for batch in tf.range(self.batch_size):
      xd_shape = [self.m]
      c_shape = [1]
      cp= input_[batch,0,:]
      for d in range(1,self.n):
        c_shape.insert(0,self.m)
        xd_shape.insert(0,1)
        xd = tf.reshape(input_[batch,d,:], (xd_shape))
        c = tf.reshape(cp,(c_shape))
        cp = tf.matmul(c , xd)
      flat_cp = tf.reshape(cp,(1, self.m**self.n))
      CP.append(flat_cp)
    return tf.reshape(tf.stack(CP), (self.batch_size, self.m**self.n))

  def compute_output_shape(self, batch_input_shape):
    return tf.TensorShape([self.batch_size, self.m ** self.n])

X_train = np.random.uniform(0, 1, (200, 16, 3))
X_test = np.random.uniform(0, 1, (200, 16, 3))
y_train = np.random.uniform(0, 1, (200,))
y_test = np.random.uniform(0, 1, (200,))

Model = keras.models.Sequential()
Model.add(WeightedLayer(n_input=16, n_memb=3,input_shape=(16, 3)))
Model.compile(loss='mean_squared_error', optimizer='adam')
Model.fit(X_train, y_train,epochs=20,batch_size=10,validation_data=(X_test, y_test))

I am getting a following error:

      /usr/local/lib/python3.7/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
      693       except Exception as e:  # pylint:disable=broad-except
      694         if hasattr(e, 'ag_error_metadata'):
--> 695           raise e.ag_error_metadata.to_exception(e)
      696         else:
      697           raise

InaccessibleTensorError: in user code:

<ipython-input-66-5c6ffada5c7a>:29 call  *
 return tf.reshape(tf.stack(CP), (self.batch_size, self.m**self.n))
 /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:206 wrapper  **
 return target(*args, **kwargs)
 /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/array_ops.py:1424 stack
 return gen_array_ops.pack(values, axis=axis, name=name)
 /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/gen_array_ops.py:6401 pack
 "Pack", values=values, axis=axis, name=name)
 /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/op_def_library.py:750 _apply_op_helper
 attrs=attr_protos, op_def=op_def)
 /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py:597 _create_op_internal
 inp = self.capture(inp)
 /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py:647 capture
 % (tensor, tensor.graph, self))

InaccessibleTensorError: The tensor 'Tensor("weighted_layer_37/while/Reshape_30:0", shape=(1, 43046721), 
dtype=float32)' cannot be accessed here: it is defined in another function or code block. Use return values, explicit 
Python locals or TensorFlow collections to access it. Defined in: FuncGraph(name=weighted_layer_37_while_body_44082, 
id=139893449741456); accessed from: FuncGraph(name=weighted_layer_37_scratch_graph, id=139893452012688).

TypeError: <lambda>() got an unexpected keyword argument 'dtype'

HI

thanks for the nice work. however, when I am trying to run it I am getting the following error. i am using python2.7 , keras 2.2.5, teras 1.14. do i have to use some other version
Traceback (most recent call last):
File "/Users/raiuli/PycharmProjects/KerasFuzzy/KerasFuzzy/case2_neuro_fuzzy.py", line 20, in
initializer_sigmas=lambda x:[[1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1],
File "/usr/local/lib/python2.7/site-packages/keras/engine/sequential.py", line 166, in add
layer(x)
File "/usr/local/lib/python2.7/site-packages/keras/engine/base_layer.py", line 425, in call
self.build(unpack_singleton(input_shapes))
File "/Users/raiuli/PycharmProjects/KerasFuzzy/KerasFuzzy/FuzzyLayer.py", line 24, in build
trainable=True)
File "/usr/local/lib/python2.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/keras/engine/base_layer.py", line 243, in add_weight
weight = K.variable(initializer(shape, dtype=dtype),
TypeError: () got an unexpected keyword argument 'dtype'

how i use dynamic dimension(None dimension) of keras.Layer in for loop?

I want build one keras Layer as follows. The input dimension is (None,16,3) and i want used it in "for loop" ,but i get this error:
ValueError: Cannot convert a partially known TensorShape to a Tensor: (?, 16, 3)
can someone help me?

class WeightedLayer(Layer):
 def __init__(self, n_input, n_memb, **kwargs):
    super(WeightedLayer, self).__init__( **kwargs)
    self.n = n_input   # 16 features
    self.m = n_memb    # 3 
    self.batch_size = None

 def build(self, batch_input_shape):
    #self.batch_size = batch_input_shape[0]
    self.batch_size = tf.shape(batch_input_shape)[0]
    super(WeightedLayer, self).build(batch_input_shape)
    
    
    
 def call(self, input_):
    CP = []
    for batch in range(self.batch_size):
        xd_shape = [self.m]
        c_shape = [1]
        cp = input_[batch,0,:]
        for d in range(1,self.n):
            c_shape.insert(0,self.m)
            xd_shape.insert(0,1)
            xd = tf.reshape(input_[batch,d,:], (xd_shape))
            c = tf.reshape(cp,(c_shape))
            cp = tf.matmul(c , xd)

        flat_cp = tf.reshape(cp,(1, self.m**self.n))
        CP.append(flat_cp)

    return tf.reshape(tf.stack(CP), (self.batch_size, self.m**self.n))

 def compute_output_shape(self,batch_input_shape):
  return tf.TensorShape([self.batch_size, self.m** self.n])

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.