Tensorflow keras矩阵尺寸与非常简单的模型不兼容

2021-11-29 22:39:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用keras对线性模型和logistic模型进行编码,并用相同的数据进行训练,但遇到了这个令人困惑的错误。下面是代码和错误消息。你知道吗

import tensorflow as tf
from tensorflow import keras as tfk
import pandas as pd


def build_model(n_features, **kwargs):
    model = tfk.models.Sequential([
        tfk.layers.Dense(1, input_shape=[n_features, ], **kwargs)
    ])
    optimizer = tfk.optimizers.SGD()
    model.compile(loss=model, optimizer=optimizer, metrics=[tfk.metrics.binary_accuracy])
    return model


if __name__ == '__main__':
    data = get_data()
    train_x, train_y, test_x, test_y = process_data(data)


    class PrintDot(tfk.callbacks.Callback):
        def on_epoch_end(self, epoch, logs):
            if epoch % 100 == 0:
                print('')
            print('.', end='')


    EPOCHS = 1000
    BATCH_SIZE = None
    d = len(train_x.keys())

    linear = build_model(d)
    sigmoid = build_model(d, activation=tfk.activations.sigmoid)

    print(train_x.shape)
    print(train_y.shape)
    print(test_x.shape)
    print(test_y.shape)
    print(linear.summary())
    print(sigmoid.summary())

    linear_res = linear.fit(
        train_x, train_y, batch_size=BATCH_SIZE,
        epochs=EPOCHS, validation_split=0.2, verbose=0,
        callbacks=[PrintDot()])
    sigmoid_res = sigmoid.fit(
        train_x, train_y, batch_size=BATCH_SIZE,
        epochs=EPOCHS, validation_split=0.2, verbose=0,
        callbacks=[PrintDot()])

    loss_linear, acc_linear = linear.evaluate(test_x, test_y, verbose=0)
    loss_sigmoid, acc_sigmoid = sigmoid.evaluate(test_x, test_y, verbose=0)
    print("""
    Linear: loss = {.2f} accuracy = {.2f}
    Logistic: loss = {.2f} accuracy = {.2f}
    """.format(loss_linear, acc_linear, loss_sigmoid, acc_sigmoid))

这里是数据和模型摘要的形状,看起来一点也不错。你知道吗

(736, 15)
(736,)
(184, 15)
(184,)
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 1)                 16        
=================================================================
Total params: 16
Trainable params: 16
Non-trainable params: 0
_________________________________________________________________
None
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 1)                 16        
=================================================================
Total params: 16
Trainable params: 16
Non-trainable params: 0
_________________________________________________________________
None

这产生了一个错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [32,1], In[1]: [15,1]
     [[{{node loss/dense_loss/sequential/dense/MatMul}}]]
     [[{{node ConstantFoldingCtrl/loss/dense_loss/broadcast_weights/assert_broadcastable/AssertGuard/Switch_0}}]]

我认为32是默认的批大小,15是数据的维度/#列,但是为什么会有[15,1]的数组呢?你知道吗

以下是tensorflow的详细错误消息:

2019-07-09 14:47:57.381250: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2019-07-09 14:47:57.636045: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties: 
name: GeForce GTX 1060 major: 6 minor: 1 memoryClockRate(GHz): 1.6705
pciBusID: 0000:01:00.0
totalMemory: 6.00GiB freeMemory: 4.97GiB
2019-07-09 14:47:57.636491: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
2019-07-09 14:47:58.354913: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-07-09 14:47:58.355175: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990]      0 
2019-07-09 14:47:58.355332: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0:   N 
2019-07-09 14:47:58.355663: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4716 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0, compute capability: 6.1)
2019-07-09 14:47:58.953351: I tensorflow/stream_executor/dso_loader.cc:152] successfully opened CUDA library cublas64_100.dll locally
2019-07-09 14:47:59.396889: E tensorflow/stream_executor/cuda/cuda_blas.cc:510] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2019-07-09 14:47:59.397449: E tensorflow/stream_executor/cuda/cuda_blas.cc:510] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2019-07-09 14:47:59.399714: E tensorflow/stream_executor/cuda/cuda_blas.cc:510] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2019-07-09 14:47:59.402435: E tensorflow/stream_executor/cuda/cuda_blas.cc:510] failed to create cublas handle: CUBLAS_STATUS_ALLOC_FAILED
2019-07-09 14:47:59.402714: W tensorflow/stream_executor/stream.cc:2130] attempting to perform BLAS operation using StreamExecutor without BLAS support
Traceback (most recent call last):
  File "C:/Users/charl/PycharmProjects/cs229_models/keras_logistic_regression.py", line 168, in <module>
    callbacks=[PrintDot()])
  File "C:\Users\charl\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\engine\training.py", line 880, in fit
    validation_steps=validation_steps)
  File "C:\Users\charl\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py", line 329, in model_iteration
    batch_outs = f(ins_batch)
  File "C:\Users\charl\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\keras\backend.py", line 3076, in __call__
    run_metadata=self.run_metadata)
  File "C:\Users\charl\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\client\session.py", line 1439, in __call__
    run_metadata_ptr)
  File "C:\Users\charl\Anaconda3\envs\tensorflow-gpu\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 528, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Matrix size-incompatible: In[0]: [32,1], In[1]: [15,1]
     [[{{node loss/dense_loss/sequential/dense/MatMul}}]]
     [[{{node ConstantFoldingCtrl/loss/dense_loss/broadcast_weights/assert_broadcastable/AssertGuard/Switch_0}}]]