Friday, May 14, 2021

 Numpy  slicing pattern


The general numpy slicing pattern is : 

        array[rows_start : rows_end + 1 : rows_step , col_start : col_end + 1: col_step]

default start is 0

default end is length of array

default step is 1 

each of the above is optional, except the first colon [:] 



mydata = np.array( [ [1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
print(mydata)
print(mydata[:])  #all parameters left to default, will print full array , same as print(mydata)
print("\n\nOdd rows only \n{}\n".format(mydata[0::2]))
print("\n\nOdd columns only \n{}\n".format(mydata[:,0::2]))
print("\n\nEven rows and even columns \n{}\n".format(mydata[1::2,1::2]))
print("\n\nFirst two columns only \n{}\n".format(mydata[:,:2])) # [:,:2] => [:,0:2:1]
print("\n\nOnly second column of all rows\n{}\n".format(mydata[:,2])) #specific column
print("\n\nAll columns from second column onwards\n{}\n".format(mydata[:,2:])) # [:,:2] => [:,2:(len):1]
# note the difference between [:,2] and [:,2:] in above two cases

print("-------------------------Array Attributes----------------------")
#examine the array attributes
# Print out memory address
print("Memory Address: {} ".format(mydata.data))

# Print out the shape
print("Shape: {} ".format(mydata.shape))
# Print out the data type
print("Data Type: {} ".format(mydata.dtype))
# Print out the stride
print("Strides: {}".format(mydata.strides))
# Print the number of dimensions
print("Number of Dimensions: {}".format(mydata.ndim))

# Print the number of elements
print("Number of elements: {}".format(mydata.size))
# Print information about memory layout
print("Flags: {}".format(mydata.flags))
# Print the length of one array element in bytes
print("Size of single array element:{}".format(mydata.itemsize))
# Print the total consumed bytes by all elements
print("Total consumed bytes: {}".format(mydata.nbytes))






Output:

[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [13 14 15 16]] [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12] [13 14 15 16]] Odd rows only [[ 1 2 3 4] [ 9 10 11 12]] Odd columns only [[ 1 3] [ 5 7] [ 9 11] [13 15]] Even rows and even columns [[ 6 8] [14 16]] First two columns only [[ 1 2] [ 5 6] [ 9 10] [13 14]] Only second column of all rows [ 3 7 11 15] All columns from second column onwards [[ 3 4] [ 7 8] [11 12] [15 16]] -------------------------Array Attributes---------------------- Memory Address: <memory at 0x7fd113ace910> Shape: (4, 4) Data Type: int64 Strides: (32, 8) Number of Dimensions: 2 Number of elements: 16 Flags: C_CONTIGUOUS : True F_CONTIGUOUS : False OWNDATA : True WRITEABLE : True ALIGNED : True WRITEBACKIFCOPY : False UPDATEIFCOPY : False Size of single array element:8 Total consumed bytes: 128

PIMA INDIAN DIABETES DATASET 


import  tensorflow as tf 
import numpy as np
from tensorflow import keras
import os 


pimadsurl = "https://raw.githubusercontent.com/jbrownlee/Datasets/master/pima-indians-diabetes.data.csv"
filepath = "/content/pimads.csv"
if (os.path.exists(filepath) == False) : 
  tf.keras.utils.get_file(filepath , origin = pimadsurl )



# # first neural network with keras tutorial
# from numpy import loadtxt
# from keras.models import Sequential
# from keras.layers import Dense
# load the dataset
dataset = np.loadtxt(filepath, delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:,0:8]
y = dataset[:,8]
# define the keras model
model = keras.models.Sequential()
model.add(keras.layers.Dense(12, input_dim=8, activation='relu'))
model.add(keras.layers.Dense(8, activation='relu'))
model.add(keras.layers.Dense(1, activation='sigmoid'))
# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# fit the keras model on the dataset
model.fit(X, y, epochs=150, batch_size=10, 
          verbose=0 #verbose = 0 means do not show the progressbar
          )
# evaluate the keras model
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))

# make class predictions with the model
predictions = model.predict_classes(X)
# summarize the first 5 cases
for i in range(5):
  print('%s => %d (expected %d)' % (X[i].tolist(), predictions[i], y[i]))


# /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/sequential.py:450: UserWarning: `model.predict_classes()` is deprecated and will be removed after 2021-01-01. Please use instead:* `np.argmax(model.predict(x), axis=-1)`,   if your model does multi-class classification   (e.g. if it uses a `softmax` last-layer activation).* `(model.predict(x) > 0.5).astype("int32")`,   if your model does binary classification   (e.g. if it uses a `sigmoid` last-layer activation).
#   warnings.warn('`model.predict_classes()` is deprecated and '

 AttributeError: 'Model' object has no attribute 'predict_classes'


Models in keras, both Sequential and Functional, provide following predictions
functions: 
  • predict()
  • predict_on_batches()
  • predict_step()
Apart from this, Sequential model provides two addtional methods: 
  • predict_classes()
  • predict_proba()
These functions directly predict the resultant class or class probability, 
without needing any conversion like numpy.argmax()
However, predict_classes() and predict_proba() are not supported by 
functional model and you will get following error if you try to implement it on
functional model: 
AttributeError: 'Model' object has no attribute 'predict_classes'

Thursday, May 13, 2021

 

Difference between keras Sequential and Functional API


Sr. No.Sequential APIFunctional API
1Syntactical : Declared using keras.Sequential([]) functionDeclared using keras.Model class 
2Syntactical : Addition of layers is by using .add interface or simply by using array like syntaxThere is no addition of layers, you simply declare the model's input and output layers. If there are more layers required in between, they have to be added to input layer using functional syntax
3Supports only linear graphs of layers (that means branching is not supported)Supports non-linear graphs of layers (that means layers can branch out and merge)
4Supports only a single input layerSupports multiple input layers
5Supports only a single output layerSupports multiple output layers
6Supports no sharing of layers (it is not needed also since layer can communicate only to one input and one output layer)Layers can be shared across other layers down the chain
7Simplistic and non-flexibleFlexible and supports more complex scenarios
8Easy to set-up and enough for most of the scenariosComparatively complex to set-up (if scenarios are complex)
Beneficial as also only option for complex scenarios
9Provides two prediction functons,
  • predict_classes
  • predict_proba. 
predict_classes outputs a numpy array of class predictions.
predict_proba outputs a numpy array of class probability predictions. 

Apart from these two it also supports the prediction functions
  • predict,
  • predict_on_batches
  • predict_step
Functional model does NOT provide the shortcuts predict_classes and predict_proba. 

It only provides the common prediction functions:
  • predict,
  • predict_on_batches
  • predict_step

Note 1: Both Sequential and Functional models earlier supported a method called predict_generator, to be used with generators. However, this method is now deprecated and the functionality is merged with predict function.
Note 2: predict_classes and predict_proba (which are supported by Sequential model only) only make sense in case of classification problems. 


RuntimeError: Intra op parallelism cannot be modified after initialization. 


Issue : (In google colab) the following error is flashed when trying to set number of threads.

RuntimeError: Intra op parallelism cannot be modified after initialization.


Following code is responsible for this error:


tf.config.threading.set_intra_op_parallelism_threads(1)
tf.config.threading.set_inter_op_parallelism_threads(1)

It allows to change inter op parallelism, but not intra op parallelism.


Solution: Restart the runtime and the error will go.

The error will not be flashed if the above code is already there by the time you start afresh.


Limitations of Sequential Model 

It is not straightforward to define models that may have multiple different input sources, produce multiple output destinations or models that re-use layers.

Functional model API allows you to define multiple input or output models as well as models that share layers. More than that, it allows you to define ad hoc acyclic network graphs.




KERAS FUNCTIONAL API 

from keras.models import Model

visible = Input(shape=(64,64,1))
conv1 = Conv2D(32, kernel_size=4, activation='relu')(visible)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(16, kernel_size=4, activation='relu')(pool1)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
flat = Flatten()(pool2)
hidden1 = Dense(10, activation='relu')(flat)
output = Dense(1, activation='sigmoid')(hidden1)

model = Model(inputs=visible, outputs=output)

 Laurence Moroney's first tutorial 



import  tensorflow as tf 
import numpy as np
from tensorflow import keras

xs = np.array([-1, 0, 1,2,3,4,5,6], dtype = float)
ys = np.array([-2, 1 , 4 , 7, 10, 13,16, 19], dtype = float)



model  = keras.models.Sequential([
          keras.layers.Dense(units = 1 , input_shape=[1])
])

model.compile(optimizer = "sgd" , loss = "mean_squared_error")

model.fit(xs, ys,epochs = 50)

print(model.predict([10]))

Comparing features of LangGraph vs CrewAI

LangGraph Orchestration vs CrewAI Orchestration The following table compares LangGraph and CrewAI orchestration across their menta...