goodboychan / goodboychan.github.io

jupyter blog
Apache License 2.0
129 stars 115 forks source link

Bayesian Convolutional Neural Network | Chan`s Jupyter #80

Open utterances-bot opened 2 years ago

utterances-bot commented 2 years ago

Bayesian Convolutional Neural Network | Chan`s Jupyter

In this post, we will create a Bayesian convolutional neural network to classify the famous MNIST handwritten digits. This will be a probabilistic model, designed to capture both aleatoric and epistemic uncertainty. You will test the uncertainty quantifications against a corrupted version of the dataset. This is the assignment of lecture “Probabilistic Deep Learning with Tensorflow 2” from Imperial College London.

https://goodboychan.github.io/python/coursera/tensorflow_probability/icl/2021/08/26/01-Bayesian-Convolutional-Neural-Network.html

Yasanthi1974 commented 2 years ago

I got following error

Load and inspect the MNIST dataset

(x_train, y_train, y_train_oh), (x_test, y_test, y_test_oh) = load_data('MNIST') inspect_images(data=x_train, num_images=8)

error and how recover it FileNotFoundError Traceback (most recent call last)

in () 1 # Load and inspect the MNIST dataset 2 ----> 3 (x_train, y_train, y_train_oh), (x_test, y_test, y_test_oh) = load_data('MNIST') 4 inspect_images(data=x_train, num_images=8) 1 frames /usr/local/lib/python3.7/dist-packages/numpy/lib/npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding) 415 own_fid = False 416 else: --> 417 fid = stack.enter_context(open(os_fspath(file), "rb")) 418 own_fid = True 419 FileNotFoundError: [Errno 2] No such file or directory: 'dataset/MNIST/x_train.npy'
Yasanthi1974 commented 2 years ago

please explain how to recover this error

On Tuesday, May 10, 2022, 05:31:29 AM GMT+5:30, utterances bot ***@***.***> wrote:  

Bayesian Convolutional Neural Network | Chan`s Jupyter

In this post, we will create a Bayesian convolutional neural network to classify the famous MNIST handwritten digits. This will be a probabilistic model, designed to capture both aleatoric and epistemic uncertainty. You will test the uncertainty quantifications against a corrupted version of the dataset. This is the assignment of lecture “Probabilistic Deep Learning with Tensorflow 2” from Imperial College London.

https://goodboychan.github.io/python/coursera/tensorflow_probability/icl/2021/08/26/01-Bayesian-Convolutional-Neural-Network.html

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

goodboychan commented 2 years ago

Hi, I check out the file that occurs the error, but it exists. Maybe you run this notebook on colab or specific environment(different folder tree) Check out your exact path for numpy dataset (x_train.npy)

Yasanthi1974 commented 2 years ago

Thank you very much for replying. I have created  "x_train.npy"  and has been saved in my local host. I used that file. Could  you please  send me your x_train.npy. On Tuesday, May 10, 2022, 07:08:49 AM GMT+5:30, Chanseok Kang @.***> wrote:

Hi, I check out the file that occurs the error, but it exists. Maybe you run this notebook on colab or specific environment(different folder tree) Check out your exact path for numpy dataset (x_train.npy)

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

goodboychan commented 2 years ago

did you check this file? these are same as mine.

Yasanthi1974 commented 2 years ago

Thank you very much. I will check it On Wednesday, May 11, 2022, 07:33:26 AM GMT+5:30, Chanseok Kang @.***> wrote:

did you check this file? these are same as mine.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

Yasanthi1974 commented 2 years ago

It is ok and Thank you very much for your support. But I am testing uncertainty for chest-xray data set. I want to prepare x-train.npy and other files for that. Could you please explain how to create x-train.npy for the above chest-xray data set (x_train, y_train, y_train_oh), (x_test, y_test, y_test_oh) = load_data('minst')inspect_images(data=x_train, num_images=8) Instead of above code I used following one train_dir = Path("E:/Dataset/chest_xray/train") val_dir = Path("E:/Dataset/chest_xray/val")test_dir = Path("E:/Dataset/chest_xray/test")  and  X_train = X_train.reshape (-1, img_size, img_size, 1)y_train = np.array(y_train) X_val = X_val.reshape(-1, img_size, img_size, 1)y_val = np.array(y_val) X_test = X_test.reshape(-1, img_size, img_size, 1)y_test = np.array(y_test)

On Wednesday, May 11, 2022, 07:33:26 AM GMT+5:30, Chanseok Kang ***@***.***> wrote:  

did you check this file? these are same as mine.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

goodboychan commented 2 years ago

I just borrow dataset from original course. But you can use np.save()[https://numpy.org/doc/stable/reference/generated/numpy.save.html] to generate .npy file.

Yasanthi1974 commented 2 years ago

Dear, In probabilistic.model,fit () , I got the following error: I tried to recover many times. but it was unsuccessful. Could you please help me to recover it?

y_train_oh = tf.keras.utils.to_categorical(y_train)print(y_train_oh.shape)print(X_train.shape) *(5856, 2)(5856, 150, 150, 1)*****

probabilistic_model.fit(X_train, y_train_oh, epochs=5)

Epoch 1/5

ValueError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_27640/1446621124.py in ----> 1 probabilistic_model.fit(X_train, y_train_oh, epochs=5)

~\anaconda3\lib\site-packages\keras\utils\traceback_utils.py in error_handler(*args, **kwargs) 65 except Exception as e: # pylint: disable=broad-except 66 filtered_tb = _process_traceback_frames(e.traceback) ---> 67 raise e.with_traceback(filtered_tb) from None 68 finally: 69 del filtered_tb

~\anaconda3\lib\site-packages\tensorflow\python\framework\func_graph.py in autograph_handler(*args, **kwargs) 1127 except Exception as e: # pylint:disable=broad-except 1128 if hasattr(e, "ag_error_metadata"): -> 1129 raise e.ag_error_metadata.to_exception(e) 1130 else: 1131 raise

ValueError: in user code:

File "C:\Users\USER\anaconda3\lib\site-packages\keras\engine\training.py", line 878, in train_function  *
    return step_function(self, iterator)
File "C:\Users\USER\AppData\Local\Temp/ipykernel_27640/4073651048.py", line 8, in nll  *
    return -y_pred.log_prob(y_true)
File "C:\Users\USER\anaconda3\lib\site-packages\tensorflow_probability\python\distributions\distribution.py", line 1316, in log_prob  **
    return self._call_log_prob(value, name, **kwargs)
File "C:\Users\USER\anaconda3\lib\site-packages\tensorflow_probability\python\distributions\distribution.py", line 1298, in _call_log_prob
    return self._log_prob(value, **kwargs)
File "C:\Users\USER\anaconda3\lib\site-packages\tensorflow_probability\python\layers\internal\distribution_tensor_coercible.py", line 112, in _log_prob
    return self.tensor_distribution._log_prob(value, **kwargs)
File "C:\Users\USER\anaconda3\lib\site-packages\tensorflow_probability\python\distributions\onehot_categorical.py", line 195, in _log_prob
    broadcast_shape = ps.broadcast_shape(ps.shape(logits), ps.shape(x))
File "C:\Users\USER\anaconda3\lib\site-packages\tensorflow_probability\python\internal\prefer_static.py", line 229, in broadcast_shape
    return tf.broadcast_static_shape(

ValueError: Incompatible shapes for broadcasting. Two shapes arecompatible if for each dimension pair they are eitherequal or one of them is 1. Received: (32, 10) and (32, 2).

ThanksYasanthi

goodboychan commented 2 years ago

I cannot help you unless enough dataset information is offered. Sorry.

Fatigerrr commented 2 years ago

hello,I want to ask you that the difference between DenseVariational and DenseReparameterization

goodboychan commented 2 years ago

I think that the difference between them is to define the kernel and bias. They both output variational inference as the dense layer does, but DenseVariational try to fit a "surrogate" posterior to the distribution, and DenseReparametrization assumes the kernel and/or the bias are drawn from distributions with reparametrization estimator. there is more detailed mention about the difference here Check this out.

Yasanthi1974 commented 2 years ago

Thank you On Friday, July 29, 2022 at 06:00:45 AM GMT+5:30, Chanseok Kang @.***> wrote:

I think that the difference between them is to define the kernel and bias. They both output variational inference as the dense layer does, but DenseVariational try to fit a "surrogate" posterior to the distribution, and DenseReparametrization assumes the kernel and/or the bias are drawn from distributions with reparametrization estimator. there is more detailed mention about the difference here Check this out.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>