'KerasTensor' object has no attribute 'train_on_batch' - keras

how do i solve this error or what can i use instead:
'KerasTensor' object has no attribute 'train_on_batch'
trying to build a VEA-GAN
encImg = G.predict(latent_vect)
fakeImg = G.predict(noise)
DlossTrue = D_true.train_on_batch(dataset, np.ones((batch_size, 1)))
DlossEnc = D_fromGen.train_on_batch(encImg, np.ones((batch_size, 1)))
DlossFake = D_fake.train_on_batch(fakeImg, np.zeros((batch_size, 1)))

Related

Error: `data` and `reference` should be factors with the same levels for imbalanced class

I Used SMOTE and Tomek methods for imbalanced classes that I have. I'm trying to do boosted regression tree.
It runs smoothly until I create the confusion matrix I have this error (
Error: data and reference should be factors with the same levels.
### SMOTE and Tomek
NOAA_SMOTE= read.csv("NOAA_SMOTE.csv", TRUE, ",")
train.index <- createDataPartition(NOAA_SMOTE$japon, p = .7, list = FALSE)
train <- NOAA_SMOTE[ train.index,]
test <- NOAA_SMOTE[-train.index,]
tomek = ubTomek(train[,-1], train[,1])
model_train_tomek = cbind(tomek$X,tomek$Y)
names(model_train_tomek)[1] = "japon"
removed.index = tomek$id.rm
train$japon = as.factor(train$japon)
train_tomek = train[-removed.index,]
## SMOTE after tomek links
traintomeksmote <- SMOTE(japon ~ ., train_tomek, perc.over = 2000,perc.under = 100)
fitControlSmoteTomek<- trainControl(## 10-fold CV
method = "repeatedcv",
number = 10,
repeats = 3,
## Estimate class probabilities
classProbs = TRUE,
## Evaluate performance using
## the following function
summaryFunction = twoClassSummary)
gbmGridSmoteTomek <- expand.grid(interaction.depth = c(3,4, 5, 6),
n.trees = (1:30)*50,
shrinkage = c(0.1,0.001,0.75,0.0001),
n.minobsinnode = 10)
gbmFitNOAASMOTETomek <- caret::train (make.names(japon) ~ ., data = traintomeksmote,
method = "gbm",
trControl = fitControlSmoteTomek,
distribution = "bernoulli",
verbose = FALSE,
tuneGrid = gbmGridSmoteTomek,
bag.fraction=0.5,
## Specify which metric to optimize
metric = "ROC")
test$japon = as.factor(test$japon)
PredNOAASMOTETomek <- predict(gbmFitNOAASMOTETomek, newdata= test ,type='prob')
cmSMOTETomekNOAA = confusionMatrix(PredNOAASMOTETomek , as.factor(test$japon), mode="everything")
part of the data
[enter image description here](https://i.stack.imgur.com/jPgI9.png)

pool_2d in Theano throws dimention mismatch error

While trying out the 'pool_2d' method of Theano, I got the error "Wrong number of dimensions: expected 4, got 1 with shape (16, )". The code is given below.
from theano import tensor, shared, function
from theano.tensor.signal.pool import pool_2d
import numpy
class Test:
def __init__(self):
i = tensor.dtensor4()
o = pool_2d(input = i, ws = (2, 2), ignore_border = True, stride = None, pad = (0, 0), mode = 'max')
self.pool = function([i], o)
def pool(self, iput):
iput = numpy.array(iput)
iput.shape = (1, 1, 4, 4)
print(self.pool(iput))
x = Test()
x.pool([1.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15.,16.])
What is the reason for the error here?
It was a stupid mistake I did. I assigned the Theano function to self.pool variable and then created another method with the same name as 'pool' causing name clash.

extract().to_dataframe() from a StanFit4Model does not work on pystan 2.18

I have estimated a model using pystan:
import pystan
stan_model = pystan.StanModel('stan_codes/stan_code_1.stan')
samples = stan_model.sampling(data = sdata, iter = 10, chains = 1, seed = 42)
model_results = {'mdata' : model_data, 'sdata': sdata, 'samples' : samples, 'model': stan_model}
Later I want to extract the "stuff" from the samples into a dataframe format. I am using
mdata, sdata, samples, model = [x for x in model_results.values()]
samples.extract().to_dataframe()
But I am getting an error:
AttributeError: 'collections.OrderedDict' object has no attribute 'to_dataframe'
According to the documentation the samples.extract() should have an attribute to_dataframe(), right ? Am i doing something wrong here ? I am using pystan version 2.18.0.0
Have you tried the extraction to DataFrame direct from the fit object
Something like ...
stan_model = pystan.StanModel('stan_codes/stan_code_1.stan')
samples = stan_model.sampling(data = sdata, iter = 10, chains = 1, seed = 42)
chains = samples.to_dataframe('parameter name')

search function return null odoo 8

Can't search using variable
#api.multi
def send_email(self,invoice_id):
invoice_data = self.env['account.invoice'].browse(invoice_id)
email_template_obj = self.env['email.template']
template_id = self.env.ref('multi_db.email_template_subscription_invoice', False)
report_id = self.env.ref('account.account_invoices', False)
print'invoice_id',str(invoice_data.id) #Here prints invoice_id
attach_obj = self.pool.get('ir.attachment')
attachment_id = self.env['ir.attachment'].search([('res_id','=',invoice_data.id),('res_model','=','account.invoice')])
print'attachment_id1234',attachment_id
if template_id:
values = email_template_obj.generate_email(template_id.id,invoice_id)
values['subject'] = 'Invoice for AMS registration'
values['email_to'] = invoice_data.partner_id.email
values['partner_to'] = invoice_data.partner_id
# values['attachment_ids'] = [(6, 0, report_id.id)]
values['attachment_ids'] = [(6, 0, [attachment_id.id])]
# print'values',values
mail_obj = self.env['mail.mail']
msg_id = mail_obj.create(values)
if msg_id:
mail_obj.send([msg_id])
return True
It returns ir.attachment()
But
If i hard code that value it will return id:
attachment_id = self.env['ir.attachment'].search([('res_id','=',60),('res_model','=','account.invoice')])
It returns ir.attachment(53).
How can i use variable instead of a static value?

How to overlay a line for an lm object on a ggplot2 scatterplot

I have some data,
calvarbyruno.1<-structure(list(Nominal = c(1, 3, 6, 10, 30, 50, 150, 250), Run = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1", "2", "3"), class = "factor"),
PAR = c(1.25000000000000e-05, 0.000960333333333333, 0.00205833333333334,
0.00423333333333333, 0.0322333333333334, 0.614433333333334,
1.24333333333333, 1.86333333333333), PredLin = c(-0.0119152187070942,
0.00375925114245899, 0.0272709559167888, 0.0586198956158952,
0.215364594111427, 0.372109292606959, 1.15583278508462, 1.93955627756228
), PredQuad = c(-0.0615895732702735, -0.0501563307416599,
-0.0330831368244257, -0.0104619953693943, 0.100190275883806,
0.20675348710041, 0.6782336426345, 1.04748729725370)), .Names = c("Nominal",
"Run", "PAR", "PredLin", "PredQuad"), row.names = c(NA, 8L), class = "data.frame")
calweight <- -2
for which I've created both a linear and a quadratic lm model
callin.1<-lm(PAR~Nominal,data=calvarbyruno.1,weight=Nominal^calweight)
calquad.1<-lm(PAR~Nominal+I(Nominal^2),data=calvarbyruno.1,weight=Nominal^calweight)
I can then plot my data values using ggplot2
qplot(PAR,Nominal,data=calvarbyruno.1)
But can't work out how to overlay a line representing the two lm objects... Any ideas ?
The easiest option is to use geom_smooth() and let ggplot2 fit the model for you.
ggplot(calvarbyruno.1, aes(y = PAR, x = Nominal, weight=Nominal^calweight)) +
geom_smooth(method = "lm") +
geom_smooth(method = "lm", formula = y ~ poly(x, 2), colour = "red") +
geom_point() +
coord_flip()
Or you can create a new dataset with the predicted values.
newdata <- data.frame(Nominal = pretty(calvarbyruno.1$Nominal, 100))
newdata$Linear <- predict(callin.1, newdata = newdata)
newdata$Quadratic <- predict(calquad.1, newdata = newdata)
require(reshape2)
newdata <- melt(newdata, id.vars = "Nominal", variable.name = "Model")
ggplot(calvarbyruno.1, aes(x = PAR, y = Nominal, weight=Nominal^calweight)) +
geom_line(data = newdata, aes(x = value, colour = Model)) +
geom_point()
Earlier I asked a related question and Hadley had this good answer. Using the predict function from that post you can add two columns to your data. One for each model:
calvarbyruno.1$calQuad <- predict(calquad.1)
calvarbyruno.1$callin <- predict(callin.1)
Then it's a matter of plotting the point and adding each model in as a line:
ggplot() +
geom_point(data=calvarbyruno.1, aes(PAR, Nominal), colour="green") +
geom_line(data=calvarbyruno.1, aes(calQuad, Nominal), colour="red" ) +
geom_line(data=calvarbyruno.1, aes(callin, Nominal), colour="blue" ) +
opts(aspect.ratio = 1)
And that results in this nice picture (yeah the colors could use some work):
(source: cerebralmastication.com)

Resources