I have a simple Tensorflow model which is written in python and could be trained smoothly. Here is the model I'm using:
import tensorflow as tf from tensorflow.python.framework
import ops from tensorflow.python.framework
import dtypes
import random import numpy as np
NUM_CLASSES = 102 IMAGE_HEIGHT = 224 IMAGE_WIDTH = 224 BATCH_SIZE = 25 NUM_CHANNELS = 3 LEARNING_RATE = 0.0001
with tf.Session() as sess:
images_placeholder = tf.placeholder (tf.float32,
shape=(BATCH_SIZE, IMAGE_HEIGHT,
IMAGE_WIDTH, NUM_CHANNELS), name="input") labels_placeholder = tf.placeholder (tf.float32,
shape=(BATCH_SIZE), name="label")
with tf.name_scope("conv1_1") as scope: kernel = tf.Variable (tf.truncated_normal([3, 3, 3, 64], dtype=tf.float32, stddev=1e-2),
name="weights") conv = tf.nn.conv2d (images_placeholder, kernel, [1, 1, 1, 1], padding='SAME') biases = tf.Variable (tf.constant(0.0, shape=[64], dtype=tf.float32),
trainable=True, name='biases') out = tf.nn.bias_add (conv, biases) conv1_1 = tf.nn.relu (out, name=scope)
pool1 = tf.nn.max_pool (conv1_1,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
name='pool1')
with tf.name_scope('conv2_1') as scope: kernel = tf.Variable (tf.truncated_normal([3, 3, 64, 128], dtype=tf.float32,
stddev=1e-2), name='weights') conv = tf.nn.conv2d (pool1, kernel, [1, 1, 1, 1], padding='SAME') biases = tf.Variable (tf.constant(0.0, shape=[128], dtype=tf.float32),
trainable=True, name='biases') out = tf.nn.bias_add (conv, biases) conv2_1 = tf.nn.relu (out, name=scope)
pool2 = tf.nn.max_pool (conv2_1,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
name='pool2')
with tf.name_scope('conv3_1') as scope: kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 256], dtype=tf.float32,
stddev=1e-2), name='weights') conv = tf.nn.conv2d(pool2, kernel, [1, 1, 1, 1], padding='SAME') biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32),
trainable=True, name='biases') out = tf.nn.bias_add(conv, biases) conv3_1 = tf.nn.relu(out, name=scope)
pool3 = tf.nn.max_pool (conv3_1,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
name='pool3')
with tf.name_scope('conv4_1') as scope: kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 512], dtype=tf.float32,
stddev=1e-2), name='weights') conv = tf.nn.conv2d(pool3, kernel, [1, 1, 1, 1], padding='SAME') biases = tf.Variable(tf.constant(0.0, shape=[512], dtype=tf.float32),
trainable=True, name='biases') out = tf.nn.bias_add(conv, biases) conv4_1 = tf.nn.relu(out, name=scope)
pool4 = tf.nn.max_pool (conv4_1,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
name='pool4')
with tf.name_scope('mentee_conv5_1') as scope: kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512], dtype=tf.float32,
stddev=1e-2), name='weights') conv = tf.nn.conv2d(pool4, kernel, [1, 1, 1, 1], padding='SAME') biases = tf.Variable(tf.constant(0.0, shape=[512], dtype=tf.float32),
trainable=True, name='biases') out = tf.nn.bias_add(conv, biases) conv5_1 = tf.nn.relu(out, name=scope)
pool5 = tf.nn.max_pool (conv5_1,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='SAME',
name='pool5')
with tf.name_scope('fc1') as scope: shape = int(np.prod(pool5.get_shape()[1:])) fc1w = tf.Variable(tf.truncated_normal([shape, 4096], dtype=tf.float32,
stddev=1e-2), name='weights') fc1b = tf.Variable(tf.constant(1.0, shape=[4096], dtype=tf.float32),
trainable=True, name='biases') pool5_flat = tf.reshape(pool5, [-1, shape]) fc1l = tf.nn.bias_add(tf.matmul(pool5_flat, fc1w), fc1b) fc1 = tf.nn.relu(fc1l)
fc1 = tf.nn.dropout(fc1, 0.5)
labels = tf.to_int64(labels_placeholder) cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits (labels=labels,
logits=fc1, name="xentropy") loss = tf.reduce_mean (cross_entropy, name='loss')
optimizer = tf.train.AdamOptimizer (LEARNING_RATE) global_step = tf.Variable (0, name='global_step', trainable=False) train_op = optimizer.minimize (loss, global_step=global_step, name="train")
init = tf.initialize_variables (tf.all_variables(), name='init_all_vars_op') tf.train.write_graph (sess.graph_def, "models/", "graph.pb", as_text=False)
I'm exporting the model into the protobuf version and then load it into my C++ code, in order to train the model using C++ API instead of Python. Unfortunately I'll get an error regarding the SparseSoftmaxCrossEntropy operation as:
E tensorflow/core/common_runtime/executor.cc:594] Executor failed to create kernel. Invalid argument: NodeDef mentions attr 'message' not in Op<name=PreventGradient; signature=input:T -> output:T; attr=T:type>; NodeDef: gradients/xentropy/xentropy_grad/PreventGradient = PreventGradient[T=DT_FLOAT, message="Currently there is no way to take the second derivative of sparse_softmax_cross_entropy_with_logits due to the fused implementation\'s interaction with tf.gradients()", _device="/job:localhost/replica:0/task:0/cpu:0"](xentropy/xentropy:1)
I have checked tensorflow source code and realized the gradient caclulation for this operation has not been implemented. But I'm wondering how python version succeeds with no issue, but in C++ it tries to calculate the gradient?
Related
I have the code using keras 1.2 and tensorflow 1.1. I have run it but with error
import numpy as np
import keras
from keras import backend as K
from keras import initializers
from keras.models import Sequential, Model, load_model, save_model
from keras.layers.core import Dense, Lambda, Activation
from keras.layers import Embedding, Input, Dense, Multiply, Reshape, Flatten
from keras.optimizers import Adagrad, Adam, SGD, RMSprop
from keras.regularizers import l2
from sklearn.metrics import average_precision_score
from sklearn.metrics import auc
def init_normal(shape, name=None):
return initializers.lecun_uniform(seed=None)
def get_model(num_a, num_b, num_c, dim, regs=[0,0,0]):
a = Input(shape=(1,), dtype='int32', name = 'a')
b = Input(shape=(1,), dtype='int32', name = 'b')
c = Input(shape=(1,), dtype='int32', name = 'c')
Embedding_a = Embedding(input_dim = num_a, output_dim = dim,
embeddings_initializer='uniform', W_regularizer = l2(regs[0]), input_length=1)
Embedding_b = Embedding(input_dim = num_b, output_dim = dim,
embeddings_initializer='uniform', W_regularizer = l2(regs[1]), input_length=1)
Embedding_c = Embedding(input_dim = num_c, output_dim = dim,
embeddings_initializer='uniform', W_regularizer = l2(regs[2]), input_length=1)
a_latent = Flatten()(Embedding_a(a))
b_latent = Flatten()(Embedding_b(b))
c_latent = Flatten()(Embedding_c(c))
predict_vector = Multiply()([a_latent, b_latent, b_latent])
prediction = Dense(1, activation='sigmoid', init='lecun_uniform', name = 'prediction')(predict_vector)
model = Model(input=[a, b, c], output=prediction)
return model
def evaluate_model(model, test_pos, test_neg):
global _model
global _test_pos
global _test_neg
_model = model
_test_pos = test_pos
_test_neg = test_neg
print(_test_neg)
a, b, c, labels = [],[],[],[]
for item in _test_pos:
a.append(item[0])
b.append(item[1])
c.append(item[2])
labels.append(1)
for item in _test_neg:
a.append(item[0])
b.append(item[1])
c.append(item[2])
labels.append(0)
a = np.array(a)
b = np.array(b)
c = np.array(c)
predictions = _model.predict([a, b, c],
batch_size=100, verbose=0)
return average_precision_score(labels, predictions), auc(labels, predictions)
model = get_model(4, 8, 12, 2, [0,0,0])
model.compile(optimizer=Adam(lr=0.001), loss='binary_crossentropy')
pos_test = [[0, 0, 2], [4, 8, 8], [2, 5, 4], [0, 0, 0]]
neg_test = [[3, 3, 2], [2, 1, 8], [1, 4, 1], [3, 3, 12]]
aupr, auc = evaluate_model(model, pos_test, neg_test)
print(aupr, auc)
However, It give me error:any way to fix it?
InvalidArgumentError (see above for traceback): indices[1,0] = 4 is not in [0, 4)
[[Node: embedding_4/embedding_lookup = Gather[Tindices=DT_INT32, Tparams=DT_FLOAT, _class=["loc:#embedding_4/embeddings"], validate_indices=true, _device="/job:localhost/replica:0/task:0/cpu:0"](embedding_4/embeddings/read, _recv_a_1_0)]]
The problem is, you defined embedding input_dim as 4, 8 and 12 while it should be is 5, 9, 13. Because input_dim in embedding should be max_index + 1. It is also clearly mentioned in Keras docs:
Size of the vocabulary, i.e. maximum integer index + 1.
How to fix the issue?
Change get_model method to:
model = get_model(5, 9, 13, 2, [0, 0, 0])
Or alternatively change index of data to:
pos_test = [[0, 0, 2], [3, 7, 7], [2, 5, 4], [0, 0, 0]]
neg_test = [[3, 3, 2], [2, 1, 7], [1, 4, 1], [3, 3, 11]]
I'm trying to embed Python in my C++ code.
Now what I'm trying to do is use the function scipy.stats.anderson_ksamp that takes as input a "sequence of 1-D array_like".
the thing is that in Python one would call the function in this simple way:
from scipy import stats
# two example np-arrays
samp1 = np.array([1,2,3,4,5,6,7,8,9,0])
samp2 = np.array([0,5,6,7,3,9,2,7,1,7])
ander_out1, ander_out2, ander_out3 = stats.anderson_ksamp([samp1, samp2])
print("Anderson statistic: %s, critical values: %s, significance level: %s"% (ander_out1, ander_out2, ander_out3))
In C++ instead I call this function in this way:
#include <Python.h>
#include <numpy/arrayobject.h>
std::vector<int> sample1 = {1,2,3,4,5,6,7,8,9,0};
std::vector<int> sample2 = {0,5,6,7,3,9,2,7,1,7};
Py_Initialize();
import_array();
npy_intp dim1[] = {(npy_intp)sample1.size()};
PyObject* np_array1 = PyArray_SimpleNewFromData(1, dim1, NPY_INT, &sample1[0]);
npy_intp dim2[] = {(npy_intp)sample2.size()};
PyObject* np_array2 = PyArray_SimpleNewFromData(1, dim2, NPY_INT, &sample2[0]);
pModule2 = PyImport_ImportModule("scipy.stats");
if (pModule2 != NULL) {
pValue3 = PyObject_CallMethod(pModule2, "anderson_ksamp", "O", /*HERE GOES THE 1-D SEQUENCE*/);
if (pValue3 != NULL) {
Py_ssize_t pValue3Size = PyTuple_Size(pValue3); // size of the tuple
printf("Anderson tuple: (");
for (Py_ssize_t j = 0; j < pValue3Size; j++) {
pElem3 = PyTuple_GetItem(pValue3, j); // getting item j of tuple
printf("%f", PyFloat_AsDouble(pElem3)); // printing item j
if (j!=pValue3Size-1) {
printf(", ");
}
}
printf(")\n");
} else {
printf("Call to anderson_ksamp failed.\n");
PyErr_Print();
}
}
What I miss is how to "merge" the two vectors sample1 and sample2 in order to make a Python object that resembles the Python statement [samp1, samp2].
I tried to create a 2-D vector in C++
std::vector<std::vector<int>> sample3 = {{1,2,3,4,5,6,7,8,9,0}, {0,5,6,7,3,9,2,7,1,7}};
and then use
npy_intp dim3[] = {10, 2};
PyObject* both_np_arrays = PyArray_SimpleNewFromData(2, dim3, NPY_INT, &sample3);
in order to create the object I need. But the object I get is something like this:
array([[ 8003984, 1],
[ 8004032, 1],
[ 8004032, 1],
[50571184, 1],
[50411920, 1],
[ 0, 0],
[ 0, 0],
[ 0, 0],
[ 0, 0],
[ 0, 0]], dtype=int32)
That is definitely not what I want.
I've also tried to do it this way:
int sample3[10][2] = {{1,0}, {2,5}, {3,6}, {4,7}, {5,3}, {6,9}, {7,2}, {8,7}, {9,1}, {0,7}};
npy_intp dim3[] = {10, 2};
PyObject* both_np_arrays = PyArray_SimpleNewFromData(2, dim3, NPY_INT, &sample3);
but the result is this:
array([[1, 0],
[2, 5],
[3, 6],
[4, 7],
[5, 3],
[6, 9],
[7, 2],
[8, 7],
[9, 1],
[0, 7]], dtype=int32)
so, again, not what I need.
I think that to use the function properly I'd need to obtain something like this:
array([[1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
[0, 5, 6, 7, 3, 9, 2, 7, 1, 7]], dtype=int32)
How can I do it?
I have a matrix and 3D tensor defined as below :
import numpy as np
import theano
import theano.tensor as T
a = T.matrix('a', dtype='float32')
c = T.tensor3('c',dtype='float32')
d = T.batched_dot(c, a)
g = theano.function([a,c],d)
Y = [[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0]], [[0, 0 ,0, 0], [0, 1, 0, 0],[0, 0, 1, 0],[0, 0, 0, 1]]]
X = [[ 0.5052417 , 0.22012063, 0.21787818, 0.41821062, 1, 1, 1, 0], [ 0.48668074, 0.26137591, 0.240702 , 0.41308364, 0, 1, 1, 1]]
x = np.array(X, dtype='float32')
y = np.array(Y, dtype='float32')
print g(x[:,0:4], y)
Although it gives correct answer in the end, but in the middle it shows many error as
ValueError: get_scalar_constant_value detected deterministic IndexError: x.shape[2] when x.ndim=2. x=Subtensor{int64}.0
ERROR (theano.gof.opt): Optimization failure due to: local_gpua_gemmbatch
ERROR (theano.gof.opt): node: BatchedDot(c, a)
ERROR (theano.gof.opt): TRACEBACK:
ValueError: get_scalar_constant_value detected deterministic IndexError: x.shape[2] when x.ndim=2. x=Subtensor{int64}.0
My expected output is
[[ 0.50524169 0.22012062 0.21787818 0. ]
[ 0. 0.2613759 0.240702 0.41308364]]
How can I correctly multiply those two ?
I'm doing text matching using tensorflow, before i call tf.nn.embedding_lookup(word_embedding_matrix, combine_result), I have to combine some words from 2 sentence(get m words from sentence S1 and also get m words from sentence S2, then combine them together as "combine_result"), but when the code gose to tf.nn.embedding_lookup(word_embedding_matrix, combine_result) it gives me the error:
ValueError: Tensor("Reshape_7:0", shape=(1, 6), dtype=int32) must be
from the same graph as Tensor("word_embedding_matrix:0", shape=(26320,
50), dtype=float32_ref).
the code is as bellow:
import tensorflow as tf
import numpy as np
import os
import time
import datetime
import data_helpers
NUM_CLASS = 2
SEQUENCE_LENGTH = 47
# Placeholders for input, output and dropout
input_x = tf.placeholder(tf.int32, [None, 2, SEQUENCE_LENGTH], name="input_x")
input_y = tf.placeholder(tf.float32, [None, NUM_CLASS], name="input_y")
dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
def n_grams(text, window_size):
text_left_window = []
# text_left_window = tf.convert_to_tensor(text_left_window, dtype=tf.int32)
for z in range(SEQUENCE_LENGTH-2):
text_left = tf.slice(text, [z], [window_size])
text_left_window = tf.concat(0, [text_left_window, text_left])
text_left_window = tf.reshape(text_left_window, [-1, window_size])
return text_left_window
def inference(vocab_size, embedding_size, batch_size, slide_window_size, conv_window_size):
# # Embedding layer
word_embedding_matrix = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0),
name="word_embedding_matrix")
# convo_unit = tf.Variable(tf.random_uniform([slide_window_size*2, ], -1.0, 1.0), name="convo_unit")
text_comp_result = []
for x in range(batch_size):
# input_x_slice_reshape = [[1 1 1...]
# [2 2 2...]]
input_x_slice = tf.slice(input_x, [x, 0, 0], [1, 2, SEQUENCE_LENGTH])
input_x_slice_reshape = tf.reshape(input_x_slice, [2, SEQUENCE_LENGTH])
# text_left_flat: [294, 6, 2, 6, 2, 57, 2, 57, 147, 57, 147, 5, 147, 5, 2,...], length = SEQUENCE_LENGTH
# text_right_flat: [17, 2, 2325, 2, 2325, 5366, 2325, 5366, 81, 5366, 81, 1238,...]
text_left = tf.slice(input_x_slice_reshape, [0, 0], [1, SEQUENCE_LENGTH])
text_left_flat = tf.reshape(text_left, [-1])
text_right = tf.slice(input_x_slice_reshape, [1, 0], [1, SEQUENCE_LENGTH])
text_right_flat = tf.reshape(text_right, [-1])
# extract both text.
# text_left_window: [[294, 6, 2], [6, 2, 57], [2, 57, 147], [57, 147, 5], [147, 5, 2],...]
# text_right_window: [[17, 2, 2325], [2, 2325, 5366], [2325, 5366, 81], [5366, 81, 1238],...]
text_left_window = n_grams(text_left_flat, slide_window_size)
text_right_window = n_grams(text_right_flat, slide_window_size)
text_left_window_sha = text_left_window.get_shape()
print 'text_left_window_sha:', text_left_window_sha
# composite the slice
text_comp_list = []
# text_comp_list = tf.convert_to_tensor(text_comp_list, dtype=tf.float32)
for l in range(SEQUENCE_LENGTH-slide_window_size+1):
text_left_slice = tf.slice(text_left_window, [l, 0], [1, slide_window_size])
text_left_slice_flat = tf.reshape(text_left_slice, [-1])
for r in range(SEQUENCE_LENGTH-slide_window_size+1):
text_right_slice = tf.slice(text_right_window, [r, 0], [1, slide_window_size])
text_right_slice_flat = tf.reshape(text_right_slice, [-1])
# convo_unit = [294, 6, 2, 17, 2, 2325]
convo_unit = tf.concat(0, [text_left_slice_flat, text_right_slice_flat])
convo_unit_reshape = tf.reshape(convo_unit, [-1, slide_window_size*2])
# convo_unit_shape_val = convo_unit_reshape.get_shape()
# print 'convo_unit_shape_val:', convo_unit_shape_val
embedded_chars = tf.nn.embedding_lookup(word_embedding_matrix, convo_unit_reshape)
embedded_chars_expanded = tf.expand_dims(embedded_chars, -1)
...
could please someone help me? Thank you very much!
Yaroslav answered in a comment above - moving to an answer:
This error happens when you create new default graph. Try to do tf.reset_default_graph() before the computation and not create any more graphs (i.e., calls to tf.Graph)
I'm trying to plot a graph that shows the average call duration every day each minute for 7 days in the same plot, now I'm defining the function that will give me the data asked according to conditions which will be plotted but I'm always getting a list of empty lists.can any one help me tof ind the bug? (acc is just an example of data from the global database)
This is the function:
import time
import calendar
from datetime import datetime
from itertools import repeat
acc=[{u'switch_id': 3, u'hangup_cause_id': 7, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 1), u'duration': 32}, {u'switch_id': 3, u'hangup_cause_id': 10, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 8), u'duration': 20}, {u'switch_id': 3, u'hangup_cause_id': 10, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 10), u'duration': 17}]
t = datetime.now()
y = t.year
m = t.month
d = t.day
donnees=[]
for k in range(7):
try:
m = t.month
data=[]
liste=[]
liste_time=[]
for i in acc:
if (i["start_uepoch"].year == y and i["start_uepoch"].month == m and i["start_uepoch"].day == d-k):
liste.append([i["start_uepoch"],i["duration"]])
for q in range(24):
for mnt in range(60):
liste2=[]
ACD=0
somme_duration=0
n=0
for p in liste:
if (p[0].hour==q and p[0].minute == mnt):
liste2.append(p[1])
temps=p[0]
if len(liste2)!=0:
for j in liste2:
somme_duration+=j
n+=1
ACD=round((float(somme_duration)/n)*100)/100
liste_time.append(calendar.timegm(temps.timetuple()))
data.append(ACD)
else:
liste_time.append(calendar.timegm(temps.timetuple()))
data.append(0)
except:
pass
donnees.append(data)
print donnees
This is due to your try / except condition, if you remove it by settings temps = None after your loop it solves you issue :
import time
import calendar
from datetime import datetime
from itertools import repeat
acc=[{u'switch_id': 3, u'hangup_cause_id': 7, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 1), u'duration': 32}, {u'switch_id': 3, u'hangup_cause_id': 10, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 8), u'duration': 20}, {u'switch_id': 3, u'hangup_cause_id': 10, u'start_uepoch': datetime(2015, 5, 8, 13, 32, 10), u'duration': 17}]
t = datetime.now()
y = t.year
m = t.month
d = t.day
donnees=[]
for k in range(7):
m = t.month
data=[]
liste=[]
liste_time=[]
for i in acc:
if (i["start_uepoch"].year == y and i["start_uepoch"].month == m and i["start_uepoch"].day == d-k):
liste.append([i["start_uepoch"],i["duration"]])
for q in range(24):
for mnt in range(60):
temps = None
liste2=[]
ACD=0
somme_duration=0
n=0
for p in liste:
if (p[0].hour==q and p[0].minute == mnt):
liste2.append(p[1])
temps=p[0]
if temps:
if len(liste2)!=0:
for j in liste2:
somme_duration+=j
n+=1
ACD=round((float(somme_duration)/n)*100)/100
liste_time.append(calendar.timegm(temps.timetuple()))
data.append(ACD)
else:
liste_time.append(calendar.timegm(temps.timetuple()))
data.append(0)
donnees.append(data)
print donnees