Issue in updating weights in keras - machine-learning

I want to update the weight (filter data and bias weight) using layer.set_weight() but got this error:
You called set_weights(weights) on layer "con" with a weight list of length 3, but the layer was expecting 2 weights.
The code that I used is:
w = model.layers[0].get_weights()
It returns the following data:
[array([[[[ 7.95478702e-01, 3.62834007e-01, 8.63312304e-01,
-2.18138605e-01]],
[[ 9.54419136e-01, 7.83904433e-01, 2.39170641e-01,
-4.93528843e-01]],
[[ 3.85988206e-01, 9.14532781e-01, -1.05724104e-01,
-6.36200011e-01]]],
[[[-2.96189755e-01, 1.04245281e+00, -7.93409765e-01,
-5.18380702e-01]],
[[-1.17103405e-01, 7.04082668e-01, -8.85501146e-01,
-4.04743433e-01]],
[[ 6.19280517e-01, 5.28193831e-01, 6.80770318e-04,
2.26266444e-01]]],
[[[-7.70748794e-01, 1.13630258e-01, 2.38650933e-01,
6.83022439e-01]],
[[-4.86002900e-02, 6.83946311e-01, 4.66377288e-01,
8.29747736e-01]],
[[ 7.78391004e-01, 2.00692505e-01, 5.20951033e-01,
1.11665785e+00]]]], dtype=float32),
array([-0.011733 , 0.00292955, 0.0021829 , 0.00517058], dtype=float32)]
Now I want to modify the weight and bias of this layer using set_weight() method. How can I modify it?

Related

question for dask output when using dask.array.map_overlap

I would like to use dask.array.map_overlap to deal with the scipy interpolation function. However, I keep meeting errors that I cannot understand and hoping someone can answer this to me.
Here is the error message I have received if I want to run .compute().
ValueError: could not broadcast input array from shape (1070,0) into shape (1045,0)
To resolve the issue, I started to use .to_delayed() to check each partition outputs, and this is what I found.
Following is my python code.
Step 1. Load netCDF file through Xarray, and then output to dask.array with chunk size (400,400)
df = xr.open_dataset('./Brazil Sentinal2 Tile/' + data_file +'.nc')
lon, lat = df['lon'].data, df['lat'].data
slon = da.from_array(df['lon'], chunks=(400,400))
slat = da.from_array(df['lat'], chunks=(400,400))
data = da.from_array(df.isel(band=0).__xarray_dataarray_variable__.data, chunks=(400,400))
Step 2. declare a function for da.map_overlap use
def sumsum2(lon,lat,data, hex_res=10):
hex_col = 'hex' + str(hex_res)
lon_max, lon_min = lon.max(), lon.min()
lat_max, lat_min = lat.max(), lat.min()
b = box(lon_min, lat_min, lon_max, lat_max, ccw=True)
b = transform(lambda x, y: (y, x), b)
b = mapping(b)
target_df = pd.DataFrame(h3.polyfill( b, hex_res), columns=[hex_col])
target_df['lat'] = target_df[hex_col].apply(lambda x: h3.h3_to_geo(x)[0])
target_df['lon'] = target_df[hex_col].apply(lambda x: h3.h3_to_geo(x)[1])
tlon, tlat = target_df[['lon','lat']].values.T
abc = lNDI(points=(lon.ravel(), lat.ravel()),
values= data.ravel())(tlon,tlat)
target_df['out'] = abc
print(np.stack([tlon, tlat, abc],axis=1).shape)
return np.stack([tlon, tlat, abc],axis=1)
Step 3. Apply the da.map_overlap
b = da.map_overlap(sumsum2, slon[:1200,:1200], slat[:1200,:1200], data[:1200,:1200], depth=10, trim=True, boundary=None, align_arrays=False, dtype='float64',
)
Step 4. Using to_delayed() to test output shape
print(b.to_delayed().flatten()[0].compute().shape, )
print(b.to_delayed().flatten()[1].compute().shape)
(1065, 3)
(1045, 0)
(1090, 3)
(1070, 0)
which is saying that the output from da.map_overlap is only outputting 1-D dimension ( which is (1045,0) and (1070,0) ), while in the da.map_overlap, the output I am preparing is 2-D dimension ( which is (1065,3) and (1090,3) ).
In addition, if I turn off the trim argument, which is
c = da.map_overlap(sumsum2,
slon[:1200,:1200],
slat[:1200,:1200],
data[:1200,:1200],
depth=10,
trim=False,
boundary=None,
align_arrays=False,
dtype='float64',
)
print(c.to_delayed().flatten()[0].compute().shape, )
print(c.to_delayed().flatten()[1].compute().shape)
The output becomes
(1065, 3)
(1065, 3)
(1090, 3)
(1090, 3)
This is saying that when trim=True, I cut out everything?
because...
#-- print out the values
b.to_delayed().flatten()[0].compute()[:10,:]
(1065, 3)
array([], shape=(1045, 0), dtype=float64)
while...
#-- print out the values
c.to_delayed().flatten()[0].compute()[:10,:]
array([[ -47.83683837, -18.98359832, 1395.01848583],
[ -47.8482856 , -18.99038681, 2663.68391094],
[ -47.82800624, -18.99207069, 1465.56517187],
[ -47.81897323, -18.97919009, 2769.91556363],
[ -47.82066663, -19.00712956, 1607.85927095],
[ -47.82696896, -18.97167714, 2110.7516765 ],
[ -47.81562653, -18.98302933, 2662.72112163],
[ -47.82176881, -18.98594465, 2201.83205114],
[ -47.84567 , -18.97512514, 1283.20631652],
[ -47.84343568, -18.97270783, 1282.92117225]])
Any thoughts for this?
Thank You.
I guess I got the answer. Please let me if I am wrong.
I am not allowing to use trim=True is because I change the shape of output array (after surfing the internet, I notice that the shape of output array should be the same with the shape of input array). Since I change the shape, the dask has no idea how to deal with it so it returns the empty array to me (weird).
Instead of using trim=False, since I didn't ask cutting-out the buffer zone, it is now okay to output the return values. (although I still don't know why the dask cannot concat the chunked array, but believe is also related to shape)
The solution is using delayed function on da.concatenate, which is
delayed(da.concatenate)([e.to_delayed().flatten()[idx] for idx in range(len(e.to_delayed().flatten()))])
In this case, we are not relying on the concat function in map_overlap but use our own concat to combine the outputs we want.

tf.shape() returns a 2-d tensor instead of 1-d

In the tensorflow API tf.shape, it says
This operation returns a 1-D integer tensor representing the shape of input.
However, when I call
features = {
'k_mask': tf.VarLenFeature(tf.int64),
'features': tf.VarLenFeature(tf.int64),
'labels': tf.FixedLenFeature([3], tf.int64),
'k_ids': tf.VarLenFeature(tf.int64)
}
parsed_features = tf.parse_single_example(example_proto, features)
features_index = tf.sparse_tensor_to_dense(parsed_features['features'])
print(sess.run(tf.shape(features_index)))
I get the result of [[59]], which is a 2-D integer tensor. The feature_index can be print as
[[ 6217 5882 17223 17235 6008 3580 17233 6038 16340 6116 5458 5747
5957 5755 17238 5745 6030 6078 5786 4373 5888 16284 3574 3569
5811 6117 5748 17228 5810 5833 5823 5885 5986 6034 5756 6105
5832 6199 6087 5744 6037 5933 6095 5785 16290 6124 3559 5787
6111 3570 6109 17322 3840 5962 3566 16950 6006 3584 6011]]
I thought this is a normal [1, 59] tensor. I try the following code:
v1 = tf.constant([[4,3,1,7]])
print(sess.run(v1)) # [[4 3 1 7]]
print(sess.run(tf.shape(v1))) # [1 4]
It looks as expected.
I want transform feature_index to shape of [59,1]. Would anyone knows why the return type is 2-d and how to convert the tensor?
Finally solved as below:
features_index = tf.sparse_tensor_to_dense(parsed_features['features'])
indices = tf.reshape(features_index, [tf.shape(features_index)[0], -1])
get shape(indices) == [59,1]

Extract p-value from GARCH model (package rugarch)

I want to extract the p value of the coefficients of my garch model.
Here is an replicable exemple:
library(rugarch)
y<-rnorm(1:100)
spec <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1, 1),
submodel = NULL, external.regressors = NULL, variance.targeting = FALSE),
mean.model = list(armaOrder = c(1, 0), external.regressors = NULL, include.mean=T), distribution.model ="norm")
garch <- ugarchfit(spec=spec, data = y , solver = 'hybrid')
Results gave me:
Optimal Parameters
Estimate Std. Error t value Pr(>|t|)
mu 0.091862 0.083258 1.10334 0.269880
ar1 -0.165456 0.098624 -1.67764 0.093418
omega 0.033234 0.050870 0.65332 0.513550
alpha1 0.041305 0.051530 0.80158 0.422793
beta1 0.920773 0.079976 11.51312 0.000000
I can extract the coef by using:
coef(garch)
But does anyone know how can I extract the pvalue?
Thanks!
you can extract the a matrix of coefficients with:
garch#fit$robust.matcoef (or garch#fit$matcoef but generally speaking robust errors preferred)
Then normal matrix indexing will allow you to retrieve values of interest, such that for retrieving p-values, you will want the retrieve the fourth column as follows:
garch#fit$robust.matcoef[,4]
Hope this helps.

How to get class labels from TensorFlow prediction

I have a classification model in TF and can get a list of probabilities for the next class (preds). Now I want to select the highest element (argmax) and display its class label.
This may seems silly, but how can I get the class label that matches a position in the predictions tensor?
feed_dict={g['x']: current_char}
preds, state = sess.run([g['preds'],g['final_state']], feed_dict)
prediction = tf.argmax(preds, 1)
preds gives me a vector of predictions for each class. Surely there must be an easy way to just output the most likely class (label)?
Some info about my model:
x = tf.placeholder(tf.int32, [None, num_steps], name='input_placeholder')
y = tf.placeholder(tf.int32, [None, 1], name='labels_placeholder')
batch_size = batch_size = tf.shape(x)[0]
x_one_hot = tf.one_hot(x, num_classes)
rnn_inputs = [tf.squeeze(i, squeeze_dims=[1]) for i in
tf.split(x_one_hot, num_steps, 1)]
tmp = tf.stack(rnn_inputs)
print(tmp.get_shape())
tmp2 = tf.transpose(tmp, perm=[1, 0, 2])
print(tmp2.get_shape())
rnn_inputs = tmp2
with tf.variable_scope('softmax'):
W = tf.get_variable('W', [state_size, num_classes])
b = tf.get_variable('b', [num_classes], initializer=tf.constant_initializer(0.0))
rnn_outputs = rnn_outputs[:, num_steps - 1, :]
rnn_outputs = tf.reshape(rnn_outputs, [-1, state_size])
y_reshaped = tf.reshape(y, [-1])
logits = tf.matmul(rnn_outputs, W) + b
predictions = tf.nn.softmax(logits)
A prediction is an array of n types of classes(labels). It represents the model's "confidence" that the image corresponds to each of its classes(labels). You can check which label has the highest confidence value by using:
prediction = np.argmax(preds, 1)
After getting this highest element index using (argmax function) out of other probabilities, you need to place this index into class labels to find the exact class name associated with this index.
class_names[prediction]
Please refer to this link for more understanding.
You can use tf.reduce_max() for this. I would refer you to this answer.
Let me know if it works - will edit if it doesn't.
Mind that there are sometimes several ways to load a dataset. For instance with fashion MNIST the tutorial could lead you to use load_data() and then to create your own structure to interpret a prediction. However you can also load these data by using tensorflow_datasets.load(...) like here after installing tensorflow-datasets which gives you access to some DatasetInfo. So for instance if your prediction is 9 you can tell it's a boot with:
import tensorflow_datasets as tfds
_, ds_info = tfds.load('fashion_mnist', with_info=True)
print(ds_info.features['label'].names[9])
When you use softmax, the labels you train the model on are either numbers 0..n or one-hot encoded values. So if original labels of your data are let's say string names, you must map them to integers first and keep the mapping as a variable (such as 0 -> "apple", 1 -> "orange", 2 -> "pear" ...).
When using integers (with loss='sparse_categorical_crossentropy'), you get predictions as an array of probabilities, you just find the array index with the max value. You can use this predicted index to reverse-map to your label:
predictedIndex = np.argmax(predictions) // 2
predictedLabel = indexToLabelMap[predictedIndex] // "pear"
If you use one-hot encoded labels (with loss='categorical_crossentropy'), the predicted index corresponds with the "hot" index of your label.
Just for reference, I needed this info when I was working with MNIST dataset used in Google's Machine learning crash course. There is also a good classification tutorial in the Tensorflow docs.

Theano: expected 2, got 1 with shape (128,).')

Im trying to make a neural network. I have followed the video from
https://www.youtube.com/watch?v=S75EdAcXHKk
I have loaded the adult.data training set.
I am now on my way of training and i have these lines where the code fails.
while(epocs<5):
i = 0
for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):
print(trX.shape)
tr = trX[start:end]
print(tr.shape[0])
print(tr.shape[1])
self.cost = train(tr.reshape(tr.shape[0],tr.shape[1]), trY[start:end])
epocs+=1
I am strugling with an error message which is:
n.training()
File "C:\Users\Bjornars\PycharmProjects\cogs-118a\Project\NN\Network.py", line 101, in training
self.cost = train(tr.reshape(128,106), trY[start:end])
File "C:\Anaconda3\lib\site-packages\theano\compile\function_module.py", line 513, in call
allow_downcast=s.allow_downcast)
File "C:\Anaconda3\lib\site-packages\theano\tensor\type.py", line 169, in filter
data.shape))
TypeError: ('Bad input argument to theano function with name "C:\Users\Bjornars\PycharmProjects\cogs-118a\Project\NN\Network.py:84" at index 1(0-based)', 'Wrong number of dimensions: expected 2, got 1 with shape (128,).')
The shape of the array im sending in is (5000,106)
---Solved----
Used this, it expected array not number in trY
def preprocess(self,trDmatrix,labels):
for i in range(len(trDmatrix)):
numbers = [0.0]*2
numbers[int(labels[i])]= 1.0
labels[i] = numbers
return trDmatrix, labels

Resources