下面代码的输入形状应该是什么

时间:2019-12-22 18:03:37

标签: python tensorflow keras

在这里,我有一个非常简单的用Tensorflow编写的回归代码:

import tensorflow as tf
import numpy as np

x_train = np.array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10]],
               dtype="float32")
y_train = np.array([[-1,  1,  3,  5,  7,  9, 11, 13, 15, 17, 19]],
               dtype="float32")

loss = 'mean_squared_error'
optimizer = tf.keras.optimizers.Adam(0.1)

model = tf.keras.Sequential([
tf.keras.layers.Dense(units=1, input_shape=[1])])

model.compile(loss=loss, optimizer=optimizer)

history = model.fit(x_train, y_train, epochs=500)

运行此代码时,出现此错误:

ValueError:检查输入时出错:预期density_24_input具有形状(1,)但具有形状(11,)的数组

现在我知道如果我从x_train和y_train中删除一对括号,代码将运行正常,就像这样:

x_train = np.array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10],
               dtype="float32")

但是我的问题是,如何用相同的数据形状运行此代码? input_shape 应该是什么,这样我就不会收到上面的错误? / p>

2 个答案:

答案 0 :(得分:2)

似乎您在x和y数组中有11个示例。为了训练每个示例必须位于大小为1的子数组中:

C:\Works\flutter_udemy_course_blank2>flutter run
Using hardware rendering with device AOSP on IA Emulator. If you get graphics artifacts, consider enabling software
rendering with "--enable-software-rendering".
Launching lib\main.dart on AOSP on IA Emulator in debug mode...

Compiler message:
/C:/src/flutter/packages/flutter/lib/src/material/dropdown.dart:1039:69: Error: Type 'WidgetsBindingObserver' not found.
class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindingObserver {
                                                                    ^^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:711:55: Error: Type 'WidgetsBindingObserver' not found.
class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver {
                                                      ^^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:1205:77: Error: Type 'WidgetsBindingObserver' not found.
class _MediaQueryFromWindowsState extends State<_MediaQueryFromWindow> with WidgetsBindingObserver {
                                                                            ^^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart:1062:103: Error: Type 'WidgetsBindingObserver' not found.
class EditableTextState extends State<EditableText> with AutomaticKeepAliveClientMixin<EditableText>, WidgetsBindingObserver, TickerProviderStateMixin<EditableText> implements TextInputClient, TextSelectionDelegate {
                                                                                                      ^^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/image.dart:941:45: Error: Type 'WidgetsBindingObserver' not found.
class _ImageState extends State<Image> with WidgetsBindingObserver {
                                            ^^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart:51:69: Error: Type 'WidgetsBindingObserver' not found.
class _SemanticsDebuggerState extends State<SemanticsDebugger> with WidgetsBindingObserver {
                                                                    ^^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart:2098:10: Error: Type 'WidgetsBindingObserver' not found.
    with WidgetsBindingObserver {
         ^^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/image.dart:941:7: Error: The type 'WidgetsBindingObserver' can't be mixed in.
class _ImageState extends State<Image> with WidgetsBindingObserver {
      ^
/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart:51:7: Error: The type 'WidgetsBindingObserver' can't be mixed in.
class _SemanticsDebuggerState extends State<SemanticsDebugger> with WidgetsBindingObserver {
      ^
/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart:2097:7: Error: The type 'WidgetsBindingObserver' can't be mixed in.
class _WidgetInspectorState extends State<WidgetInspector>
      ^
/C:/src/flutter/packages/flutter/lib/src/material/dropdown.dart:1039:7: Error: The type 'WidgetsBindingObserver' can't be mixed in.
class _DropdownButtonState<T> extends State<DropdownButton<T>> with WidgetsBindingObserver {
      ^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:711:7: Error: The type 'WidgetsBindingObserver' can't be mixed in.
class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver {
      ^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:1205:7: Error: The type 'WidgetsBindingObserver' can't be mixed in.
class _MediaQueryFromWindowsState extends State<_MediaQueryFromWindow> with WidgetsBindingObserver {
      ^
/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart:1062:7: Error: The type 'WidgetsBindingObserver' can't be mixed in.
class EditableTextState extends State<EditableText> with AutomaticKeepAliveClientMixin<EditableText>, WidgetsBindingObserver, TickerProviderStateMixin<EditableText> implements TextInputClient, TextSelectionDelegate {
      ^
lib/main.dart:9:3: Error: Method not found: 'runApp'.
  runApp(new MyApp());
  ^^^^^^
/C:/src/flutter/packages/flutter/lib/src/material/dropdown.dart:1066:39: Error: The getter 'WidgetsBinding' isn't defined for the class '_DropdownButtonState<T>'.
 - '_DropdownButtonState' is from 'package:flutter/src/material/dropdown.dart' ('/C:/src/flutter/packages/flutter/lib/src/material/dropdown.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    final FocusManager focusManager = WidgetsBinding.instance.focusManager;
                                      ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/material/dropdown.dart:1073:5: Error: The getter 'WidgetsBinding' isn't defined for the class '_DropdownButtonState<T>'.
 - '_DropdownButtonState' is from 'package:flutter/src/material/dropdown.dart' ('/C:/src/flutter/packages/flutter/lib/src/material/dropdown.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    WidgetsBinding.instance.removeObserver(this);
    ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/material/dropdown.dart:1075:5: Error: The getter 'WidgetsBinding' isn't defined for the class '_DropdownButtonState<T>'.
 - '_DropdownButtonState' is from 'package:flutter/src/material/dropdown.dart' ('/C:/src/flutter/packages/flutter/lib/src/material/dropdown.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    WidgetsBinding.instance.focusManager.removeHighlightModeListener(_handleFocusHighlightModeChange);
    ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:718:31: Error: The getter 'WidgetsBinding' isn't defined for the class '_WidgetsAppState'.
 - '_WidgetsAppState' is from 'package:flutter/src/widgets/app.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    _locale = _resolveLocales(WidgetsBinding.instance.window.locales, widget.supportedLocales);
                              ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:719:5: Error: The getter 'WidgetsBinding' isn't defined for the class '_WidgetsAppState'.
 - '_WidgetsAppState' is from 'package:flutter/src/widgets/app.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    WidgetsBinding.instance.addObserver(this);
    ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:731:5: Error: The getter 'WidgetsBinding' isn't defined for the class '_WidgetsAppState'.
 - '_WidgetsAppState' is from 'package:flutter/src/widgets/app.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    WidgetsBinding.instance.removeObserver(this);
    ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:1071:23: Error: The getter 'WidgetsBinding' isn't defined for the class '_WidgetsAppState'.
 - '_WidgetsAppState' is from 'package:flutter/src/widgets/app.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
        initialRoute: WidgetsBinding.instance.window.defaultRouteName != Navigator.defaultRouteName
                      ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:1072:15: Error: The getter 'WidgetsBinding' isn't defined for the class '_WidgetsAppState'.
 - '_WidgetsAppState' is from 'package:flutter/src/widgets/app.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
            ? WidgetsBinding.instance.window.defaultRouteName
              ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:1073:38: Error: The getter 'WidgetsBinding' isn't defined for the class '_WidgetsAppState'.
 - '_WidgetsAppState' is from 'package:flutter/src/widgets/app.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
            : widget.initialRoute ?? WidgetsBinding.instance.window.defaultRouteName,
                                     ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:1209:5: Error: The getter 'WidgetsBinding' isn't defined for the class '_MediaQueryFromWindowsState'.
 - '_MediaQueryFromWindowsState' is from 'package:flutter/src/widgets/app.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    WidgetsBinding.instance.addObserver(this);
    ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:1254:39: Error: The getter 'WidgetsBinding' isn't defined for the class '_MediaQueryFromWindowsState'.
 - '_MediaQueryFromWindowsState' is from 'package:flutter/src/widgets/app.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
      data: MediaQueryData.fromWindow(WidgetsBinding.instance.window),
                                      ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart:1261:5: Error: The getter 'WidgetsBinding' isn't defined for the class '_MediaQueryFromWindowsState'.
 - '_MediaQueryFromWindowsState' is from 'package:flutter/src/widgets/app.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/app.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    WidgetsBinding.instance.removeObserver(this);
    ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/binding.dart:10:45: Error: The argument type 'Type' can't be assigned to the parameter type 'PreferredSizeWidget'.
 - 'Type' is from 'dart:core'.
 - 'PreferredSizeWidget' is from 'package:flutter/src/widgets/preferred_size.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/preferred_size.dart').
  return MaterialApp(home: Scaffold(appBar: AppBar ,),);
                                            ^
/C:/src/flutter/packages/flutter/lib/src/widgets/drag_target.dart:633:5: Error: The getter 'WidgetsBinding' isn't defined for the class '_DragAvatar<T>'.
 - '_DragAvatar' is from 'package:flutter/src/widgets/drag_target.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/drag_target.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    WidgetsBinding.instance.hitTest(result, globalPosition + feedbackOffset);
    ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart:1590:32: Error: The getter 'WidgetsBinding' isn't defined for the class 'EditableTextState'.
 - 'EditableTextState' is from 'package:flutter/src/widgets/editable_text.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    if (_lastBottomViewInset < WidgetsBinding.instance.window.viewInsets.bottom) {
                               ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart:1593:28: Error: The getter 'WidgetsBinding' isn't defined for the class 'EditableTextState'.
 - 'EditableTextState' is from 'package:flutter/src/widgets/editable_text.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    _lastBottomViewInset = WidgetsBinding.instance.window.viewInsets.bottom;
                           ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart:1712:7: Error: The getter 'WidgetsBinding' isn't defined for the class 'EditableTextState'.
 - 'EditableTextState' is from 'package:flutter/src/widgets/editable_text.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
      WidgetsBinding.instance.addObserver(this);
      ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart:1713:30: Error: The getter 'WidgetsBinding' isn't defined for the class 'EditableTextState'.
 - 'EditableTextState' is from 'package:flutter/src/widgets/editable_text.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
      _lastBottomViewInset = WidgetsBinding.instance.window.viewInsets.bottom;
                             ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart:1720:7: Error: The getter 'WidgetsBinding' isn't defined for the class 'EditableTextState'.
 - 'EditableTextState' is from 'package:flutter/src/widgets/editable_text.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/editable_text.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
      WidgetsBinding.instance.removeObserver(this);
      ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/focus_manager.dart:1152:39: Error: Getter not found: 'WidgetsBinding'.
  static FocusManager get instance => WidgetsBinding.instance.focusManager;
                                      ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/focus_manager.dart:1210:75: Error: The getter 'WidgetsBinding' isn't defined for the class 'FocusManager'.
 - 'FocusManager' is from 'package:flutter/src/widgets/focus_manager.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/focus_manager.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    _lastInteractionWasTouch ??= Platform.isAndroid || Platform.isIOS || !WidgetsBinding.instance.mouseTracker.mouseIsConnected;
                                                                          ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/focus_manager.dart:1433:31: Error: Getter not found: 'WidgetsBinding'.
FocusNode get primaryFocus => WidgetsBinding.instance.focusManager.primaryFocus;
                              ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/focus_manager.dart:1440:10: Error: Getter not found: 'WidgetsBinding'.
  assert(WidgetsBinding.instance != null);
         ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/heroes.dart:803:9: Error: The getter 'WidgetsBinding' isn't defined for the class 'HeroController'.
 - 'HeroController' is from 'package:flutter/src/widgets/heroes.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/heroes.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
        WidgetsBinding.instance.addPostFrameCallback((Duration value) {
        ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/image.dart:953:5: Error: The getter 'WidgetsBinding' isn't defined for the class '_ImageState'.
 - '_ImageState' is from 'package:flutter/src/widgets/image.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/image.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    WidgetsBinding.instance.addObserver(this);
    ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/image.dart:959:5: Error: The getter 'WidgetsBinding' isn't defined for the class '_ImageState'.
 - '_ImageState' is from 'package:flutter/src/widgets/image.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/image.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    WidgetsBinding.instance.removeObserver(this);
    ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/image.dart:991:11: Error: Superclass has no method named 'didChangeAccessibilityFeatures'.
    super.didChangeAccessibilityFeatures();
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/localizations.dart:522:7: Error: The getter 'WidgetsBinding' isn't defined for the class '_LocalizationsState'.
 - '_LocalizationsState' is from 'package:flutter/src/widgets/localizations.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/localizations.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
      WidgetsBinding.instance.deferFirstFrameReport();
      ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/localizations.dart:524:9: Error: The getter 'WidgetsBinding' isn't defined for the class '_LocalizationsState'.
 - '_LocalizationsState' is from 'package:flutter/src/widgets/localizations.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/localizations.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
        WidgetsBinding.instance.allowFirstFrameReport();
        ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/navigator.dart:2325:38: Error: The getter 'WidgetsBinding' isn't defined for the class 'NavigatorState'.
 - 'NavigatorState' is from 'package:flutter/src/widgets/navigator.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/navigator.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    _activePointers.toList().forEach(WidgetsBinding.instance.cancelPointer);
                                     ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/scroll_physics.dart:209:30: Error: Getter not found: 'WidgetsBinding'.
    velocity: 1.0 / (0.050 * WidgetsBinding.instance.window.devicePixelRatio), // logical pixels per second
                             ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/scroll_physics.dart:210:21: Error: Getter not found: 'WidgetsBinding'.
    distance: 1.0 / WidgetsBinding.instance.window.devicePixelRatio, // logical pixels
                    ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart:61:32: Error: The getter 'WidgetsBinding' isn't defined for the class '_SemanticsDebuggerState'.
 - '_SemanticsDebuggerState' is from 'package:flutter/src/widgets/semantics_debugger.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    _client = _SemanticsClient(WidgetsBinding.instance.pipelineOwner)
                               ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart:63:5: Error: The getter 'WidgetsBinding' isn't defined for the class '_SemanticsDebuggerState'.
 - '_SemanticsDebuggerState' is from 'package:flutter/src/widgets/semantics_debugger.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    WidgetsBinding.instance.addObserver(this);
    ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart:71:5: Error: The getter 'WidgetsBinding' isn't defined for the class '_SemanticsDebuggerState'.
 - '_SemanticsDebuggerState' is from 'package:flutter/src/widgets/semantics_debugger.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    WidgetsBinding.instance.removeObserver(this);
    ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart:106:51: Error: The getter 'WidgetsBinding' isn't defined for the class '_SemanticsDebuggerState'.
 - '_SemanticsDebuggerState' is from 'package:flutter/src/widgets/semantics_debugger.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
      _lastPointerDownLocation = event.position * WidgetsBinding.instance.window.devicePixelRatio;
                                                  ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart:158:39: Error: The getter 'WidgetsBinding' isn't defined for the class '_SemanticsDebuggerState'.
 - '_SemanticsDebuggerState' is from 'package:flutter/src/widgets/semantics_debugger.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
  PipelineOwner get _pipelineOwner => WidgetsBinding.instance.pipelineOwner;
                                      ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart:167:9: Error: The getter 'WidgetsBinding' isn't defined for the class '_SemanticsDebuggerState'.
 - '_SemanticsDebuggerState' is from 'package:flutter/src/widgets/semantics_debugger.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/semantics_debugger.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
        WidgetsBinding.instance.window.devicePixelRatio,
        ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart:904:11: Error: 'WidgetsBinding' isn't a type.
    final WidgetsBinding binding = WidgetsBinding.instance;
          ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart:904:36: Error: The getter 'WidgetsBinding' isn't defined for the class 'WidgetInspectorService'.
 - 'WidgetInspectorService' is from 'package:flutter/src/widgets/widget_inspector.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    final WidgetsBinding binding = WidgetsBinding.instance;
                                   ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart:1228:12: Error: The getter 'WidgetsBinding' isn't defined for the class 'WidgetInspectorService'.
 - 'WidgetInspectorService' is from 'package:flutter/src/widgets/widget_inspector.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    return WidgetsBinding.instance != null &&
           ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart:1229:12: Error: The getter 'WidgetsBinding' isn't defined for the class 'WidgetInspectorService'.
 - 'WidgetInspectorService' is from 'package:flutter/src/widgets/widget_inspector.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
           WidgetsBinding.instance.debugDidSendFirstFrameEvent;
           ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart:1592:24: Error: The getter 'WidgetsBinding' isn't defined for the class 'WidgetInspectorService'.
 - 'WidgetInspectorService' is from 'package:flutter/src/widgets/widget_inspector.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    return _nodeToJson(WidgetsBinding.instance?.renderViewElement?.toDiagnosticsNode(), _SerializationDelegate(groupName: groupName, service: this));
                       ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart:1603:7: Error: The getter 'WidgetsBinding' isn't defined for the class 'WidgetInspectorService'.
 - 'WidgetInspectorService' is from 'package:flutter/src/widgets/widget_inspector.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
      WidgetsBinding.instance?.renderViewElement?.toDiagnosticsNode(),
      ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart:2243:41: Error: The getter 'WidgetsBinding' isn't defined for the class '_WidgetInspectorState'.
 - '_WidgetInspectorState' is from 'package:flutter/src/widgets/widget_inspector.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    final Rect bounds = (Offset.zero & (WidgetsBinding.instance.window.physicalSize / WidgetsBinding.instance.window.devicePixelRatio)).deflate(_kOffScreenMargin);
                                        ^^^^^^^^^^^^^^
/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart:2243:87: Error: The getter 'WidgetsBinding' isn't defined for the class '_WidgetInspectorState'.
 - '_WidgetInspectorState' is from 'package:flutter/src/widgets/widget_inspector.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/widget_inspector.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'WidgetsBinding'.
    final Rect bounds = (Offset.zero & (WidgetsBinding.instance.window.physicalSize / WidgetsBinding.instance.window.devicePixelRatio)).deflate(_kOffScreenMargin);
                                                                                      ^^^^^^^^^^^^^^
Target kernel_snapshot failed: Exception: Errors during snapshot creation: null
build failed.

FAILURE: Build failed with an exception.

* Where:
Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 780

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 24s
Running Gradle task 'assembleDebug'...
Running Gradle task 'assembleDebug'... Done                        24.9s
Gradle task assembleDebug failed with exit code 1

答案 1 :(得分:1)

如果我了解您要使用数据格式

因此,您必须在图层[tf.keras.layers.Dense(units=11, input_dim=11)]上进行此更改。使用mean_squared_error时。您的输出应与inut_dim相同,我设置为units=11

总结。

import tensorflow as tf
import numpy as np

x_train = np.array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10]], dtype="float32")
y_train = np.array([[-1,  1,  3,  5,  7,  9, 11, 13, 15, 17, 19]], dtype="float32")

loss = 'mean_squared_error'
optimizer = tf.keras.optimizers.Adam(0.1)

model = tf.keras.Sequential([tf.keras.layers.Dense(units=11, input_dim=11)])

model.compile(loss=loss, optimizer=optimizer)

history = model.fit(x_train, y_train, epochs=500)