keras

Loading TF Records into Keras

旧时模样 提交于 2021-01-28 13:50:54
问题 I am trying to load a custom TFRecord file into my keras model. I attempted to follow this tutorial: https://medium.com/@moritzkrger/speeding-up-keras-with-tfrecord-datasets-5464f9836c36, but adapting for my use. My goal is to have the functions work similar to ImageDataGenerator from Keras. I cannot use that function because I specific metadata from the images that the generator does not grab. I'm not including that metadata here because I just need the basic network to function first. I

Only integer scalar arrays can be converted to a scalar index how to resolve

左心房为你撑大大i 提交于 2021-01-28 13:41:34
问题 File "C:/Users/Benji/PycharmProjects/Code/ANOTHER22.py", line 57, in generator x_class[np.array(y)[i]].append(np.array(x)[i]) TypeError: only integer scalar arrays can be converted to a scalar index x_class = [[] for i in range(len(np.unique(y)))] for i in range(len(x)): x_class[y[i]].append(x[i]) 来源: https://stackoverflow.com/questions/61717955/only-integer-scalar-arrays-can-be-converted-to-a-scalar-index-how-to-resolve

Make prediction with Keras model using multiple CPUs

℡╲_俬逩灬. 提交于 2021-01-28 12:04:00
问题 I am trying to make predictions with a Keras model (using Tensorflow 2.0) using multiple CPUs. I have tried this: tf.config.threading.set_intra_op_parallelism_threads(4) tf.config.threading.set_inter_op_parallelism_threads(4) While not getting an error, I am not sure if this is the right approach. Can predictions be multithreaded? Many thanks 来源: https://stackoverflow.com/questions/58974483/make-prediction-with-keras-model-using-multiple-cpus

Keras gradient wrt something else

亡梦爱人 提交于 2021-01-28 11:24:36
问题 I am working to implement the method described in the article https://drive.google.com/file/d/1s-qs-ivo_fJD9BU_tM5RY8Hv-opK4Z-H/view . The final algorithm to use is here (it is on page 6): d are units vector xhi is a non-null number D is the loss function (sparse cross-entropy in my case) The idea is to do an adversarial training, by modifying the data in the direction where the network is the most sensible to small changes and training the network with the modified data but with the same

Computing gradient of the model with modified weights

别等时光非礼了梦想. 提交于 2021-01-28 11:16:47
问题 I was implementing Sharpness Aware Minimization (SAM) using Tensorflow. The algorithm is simplified as follows Compute gradient using current weight W Compute ε according to the equation in the paper Compute gradient using the weights W + ε Update model using gradient from step 3 I have implement step 1 and 2 already, but having trouble implementing step 3 according to the code below def train_step(self, data, rho=0.05, p=2, q=2): if (1 / p) + (1 / q) != 1: raise tf.python.framework.errors

x and y can be no greater than 2-D, but have shapes (1,) and (1, 224, 224, 3)

给你一囗甜甜゛ 提交于 2021-01-28 11:11:44
问题 I am trying to visualize the prediction of VGG-16 for the cat image, Compute the top-5 scores (the 5 classes with the maximum probability), For each of these 5 scores, print the corresponding label and the corresponding probability. from keras.applications.vgg16 import preprocess_input from keras.preprocessing import image # load the image from cat class and resize it img = image.load_img('cat.jpg', target_size=(224, 224)) # convert to numpy array of (224, 224, 3) x = image.img_to_array(img)

How to wrap tensorflow graph with placeholder in keras

[亡魂溺海] 提交于 2021-01-28 11:09:41
问题 I have a tensorflow graph (stored in a protobuffer file) with placeholder operations as inputs. I want to wrap this graph as a keras layer or model. Here is an example: with tf.Graph().as_default() as gf: x = tf.placeholder(tf.float32, shape=(None, 123), name='x') c = tf.constant(100, dtype=tf.float32, name='C') y = tf.multiply(x, c, name='y') with tf.gfile.GFile("test_graph/y.pb", "wb") as f: raw = gf.as_graph_def().SerializeToString() f.write(raw) Load back as a tensorflow graph: persisted

Preparing feeding data to 1D CNN

不羁的心 提交于 2021-01-28 10:28:14
问题 I am getting into a similar problem with reshaping data for 1-D CNN: I am loading data (training and testing data sets ) from a csv file with 24,325 lines. Each line is a vector of 256 numbers - independent variables plus 11 numbers of expected outcome ( labels ) [0,0,0,0,1,0,0,0,0,0,0] I am using TensorFlow backend. The code looks like that: import matplotlib.pyplot as plt import pandas as pd import numpy as np #Importing training set training_set = pd.read_csv("Data30.csv") X_train =

Preparing feeding data to 1D CNN

帅比萌擦擦* 提交于 2021-01-28 10:27:25
问题 I am getting into a similar problem with reshaping data for 1-D CNN: I am loading data (training and testing data sets ) from a csv file with 24,325 lines. Each line is a vector of 256 numbers - independent variables plus 11 numbers of expected outcome ( labels ) [0,0,0,0,1,0,0,0,0,0,0] I am using TensorFlow backend. The code looks like that: import matplotlib.pyplot as plt import pandas as pd import numpy as np #Importing training set training_set = pd.read_csv("Data30.csv") X_train =

Keras Model - Get input in custom loss function

一曲冷凌霜 提交于 2021-01-28 10:15:55
问题 I am having trouble with Keras Custom loss function. I want to be able to access truth as a numpy array. Because it is a callback function, I think I am not in eager execution , which means I can't access it using the backend.get_value() function. i also tried different methods, but it always comes back to the fact that this 'Tensor' object doesn't exist. Do I need to create a session inside the custom loss function ? I am using Tensorflow 2.2, which is up to date. def custom_loss(y_true, y