i put the function model.sequential() from keras but then say me that module is not callable - module

from keras import models
from keras import layers
network = models.Sequential()
and then say to me that module is not callable and i dont now what to do whit the layers

Related

TF.js import error with model created using TF Lite Model Maker

I've created a model using the tutorial at https://www.tensorflow.org/lite/tutorials/model_maker_image_classification and exported it in the TF.js format:
import os
import matplotlib.pyplot as plt
import tensorflow as tf
from tflite_model_maker import image_classifier, model_spec
from tflite_model_maker.config import ExportFormat, QuantizationConfig
from tflite_model_maker.image_classifier import DataLoader
image_path = tf.keras.utils.get_file(
'flower_photos.tgz',
'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
extract=True)
image_path = os.path.join(os.path.dirname(image_path), 'flower_photos')
data = DataLoader.from_folder(image_path)
train_data, test_data = data.split(0.9)
model = image_classifier.create(train_data)
loss, accuracy = model.evaluate(test_data)
# Export model to TF.js format
model.export(export_dir='.', export_format=ExportFormat.TFJS)
When loading this model in TF.js using tf.loadLayersModel I get the following error:
Uncaught (in promise) Error: Unknown layer: HubKerasLayerV1V2.
This may be due to one of the following reasons:
1. The layer is defined in Python, in which case it needs to be
ported to TensorFlow.js or your JavaScript code.
2. The custom layer is defined in JavaScript, but is not registered
properly with tf.serialization.registerClass()
I guess the error is due to reason (1), but how can I port the HubKerasLayerV1V2 layer to TF.js?
I believe this is an issue with the model converter having issues with a partial Graph inside of a Layers model.
You can probably fix this by serializing the model to the normal SaveModel format and export the HDF5. Once you have the .h5 output, use the TensorFlow.js converter (tensorflowjs_converter) to create a purely Graph model. Then try loading with tf.loadGraphModel instead.

How can I run this custom AttentionLSTM class, built in TF 1.0, on TF 2.0?

I'm building upon the work of a colleague who built a custom AttentionLSTM layer in TF 1. I want to use TF 2.
I've changed all the import statements at the top to be from tensorflow.keras import .... But there are two that I haven't figured out how to change.
from keras.legacy import interfaces
from keras.layers import Recurrent
Both are used in the AttentionLSTM class definition once and no where else.
class AttentionLSTM(Recurrent):
#interfaces.legacy_recurrent_support
def __init__(self, units,
activation='tanh',
recurrent_activation='hard_sigmoid',
attention_activation='tanh',
use_bias=True,
kernel_initializer='glorot_uniform',
recurrent_initializer='orthogonal',
attention_initializer='orthogonal',
bias_initializer='zeros',
unit_forget_bias=True,
kernel_regularizer=None,
recurrent_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
attention_regularizer=None,
kernel_constraint=None,
recurrent_constraint=None,
bias_constraint=None,
attention_constraint=None,
dropout=0.,
recurrent_dropout=0.,
return_attention=False,
**kwargs):
...
What does the interfaces decorator do? What do I need to change so that I can use this class in TF 2?
Note: I think I should change the Recurrent import to from tensorflow.keras.layers import RNN but am worried that will mess up what the interfaces decorator does.
from keras.legacy import interfaces and from keras.layers import Recurrent These two libraries work with Keras 2.3.1.
Latest Tensorflow version has default Keras 2.4.3 version.
In order to use these two libraries downgrade your Keras to 2.3.1.
Tensorflow.keras has no such library.
And for keras.layers import Recurrent use tf.keras.layers.RNN
Take a look at Keras release note.

Strange error while creating a convolution neural network

i want to create convolutional neural network model in keras, first of all i have imported all necessary library like this
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import MaxPooling2D
than i have tried following model
model =Sequential()
model.add(Conv2D(filters=32,kernel_size=(5,5),padding='valid',input_shape=(1,28,28),activation='relu',data_format='channels_first'))
model.add(MaxPooling2D(2,2, dim_ordering='tf'))
moodel.add(Dropout(0.2))
model.add(Flattenn())
model.add(Dense(128,activation='relu'))
model.add(Dense(num_classes,activation='softmax'))
model.compile(loss='categorical_crossentropy' , optimizer='adam' , metrics=['accuracy' ])
but i have got following error :
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.layers.convolutional.Conv2D object at 0x7faaefc57438>
could you help me please to fix this error
moodel.add(Dropout(0.2))
Typo - moodel -> model
model.add(Flattenn())
Typo - Flattenn -> Flatten
And you should use Convolution2D instead of Conv2D import.
from tensorflow.keras.layers import Convolution2D
model = Sequential()
model.add(Convolution2D(filters=32,...

How do i write a custom wavelet activation function for a Wavelet Neural Network using Keras or tensorflow

Trying to build a Wavelet Neural Network using Keras/Tensorflow. For this Neural Network I am supposed to use a Wavelet function as my activation function.
I have tried doing this by simply calling creating a custom activation function. However there seems to be an issue in regards to the backpropagation
import numpy as np
import pandas as pd
import pywt
import matplotlib.pyplot as plt
import tensorflow as tf
from keras.models import Model
import keras.layers as kl
from keras.layers import Input, Dense
import keras as kr
from keras.layers import Activation
from keras import backend as K
from keras.utils.generic_utils import get_custom_objects
def custom_activation(x):
return pywt.dwt(x, 'db1') -1
get_custom_objects().update({'custom_activation':Activation(custom_activation)})
model = Sequential()
model.add(Dense(12, input_dim=8, activation=custom_activation))
model.add(Dense(8, activation=custom_activation)
model.add(Dense(1, activation=custom_activation)
i get the following error for running the code in its entirety
SyntaxError: invalid syntax
if i run
model = Sequential()
model.add(Dense(12, input_dim=8, activation=custom_activation))
model.add(Dense(8, activation=custom_activation)
i get the following error
SyntaxError: unexpected EOF while parsing
and if i run
model = Sequential()
model.add(Dense(12, input_dim=8, activation=custom_activation))
I get the following error
TypeError: Cannot convert DType to numpy.dtype
model.add() is a function call. You must close parenthesis, otherwise it is a syntax error.
These two lines in your code example will cause a syntax error.
model.add(Dense(8, activation=custom_activation)
model.add(Dense(1, activation=custom_activation)
Regarding the 2nd question:
I get the following error
TypeError: Cannot convert DType to numpy.dtype
This seems like a numpy function was invoked with the incorrect arguments. Perhaps you can try to figure out which line in the script caused the error.
Also, an activation function must be written in keras backend operations. Or you need to manually compute the gradients for it. Neural network training requires being able to compute the gradients of a function on the reverse pass in order to adjust the weights. As far as I understand it you can't just call an arbitrary python library as an activation function; you have to either re-implement its operations using tensor operations or you have the option of using python operations on eager tensors if you know how to compute the gradients manually.

Getting Cuda code from Tensorflow or Keras

I have a code in Keras (or its TF version). I want to have a CUDA code which is equivalence to it. Is there a way to get it?
I know that from Keras I can look at the basic graph topology using the following code:
# LSTM for sequence classification in the IMDB dataset
import numpy
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.embeddings import Embedding
from keras import backend as K
from keras.preprocessing import sequence
# fix random seed for reproducibility
numpy.random.seed(7)
# load the dataset but only keep the top n words, zero the rest
top_words = 5000
max_review_length = 500
# create the model
embedding_vecor_length = 32
model = Sequential()
model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))
model.add(LSTM(100))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
g = K.get_session().graph
# GIVES THE GRAPH TOPOLOGY!:
graph_def = g.as_graph_def()
Is there a way to have the .cc file that represent this code?
Thanks!
There is no functionality in TensorFlow to generate C++ CUDA source code from a graph, but the XLA framework supports ahead-of-time compilation, which generates efficient bytecode from your TensorFlow graph, which you can then execute on your CUDA-capable GPU.